Estoy construyendo una aplicación .NET 4 WPF utilizando primero el código de Entity Framework y SQL Server Compact 4.0. Estoy intentando llamar DbContext.SaveChanges()
en un subproceso de fondo para evitar el bloqueo de la interfaz de usuario, pero estoy en ocasiones conseguir la siguiente excepción:SQL Server Compact Edition 4 - AccessViolationException
System.AccessViolationException occurred
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Data.SqlServerCe
StackTrace:
at System.Data.SqlServerCe.NativeMethodsHelper.OpenStore(IntPtr pOpenInfo, IntPtr pfnOnFlushFailure, IntPtr& pStoreService, IntPtr& pStoreServer, IntPtr& pQpServices, IntPtr& pSeStore, IntPtr& pTx, IntPtr& pQpDatabase, IntPtr& pQpSession, IntPtr& pStoreEvents, IntPtr& pError)
at System.Data.SqlServerCe.NativeMethods.OpenStore(IntPtr pOpenInfo, IntPtr pfnOnFlushFailure, IntPtr& pStoreService, IntPtr& pStoreServer, IntPtr& pQpServices, IntPtr& pSeStore, IntPtr& pTx, IntPtr& pQpDatabase, IntPtr& pQpSession, IntPtr& pStoreEvents, IntPtr& pError)
at System.Data.SqlServerCe.SqlCeConnection.Open(Boolean silent)
at System.Data.SqlServerCe.SqlCeConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
at System.Data.EntityClient.EntityConnection.Open()
at System.Data.Objects.ObjectContext.EnsureConnection()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at SourceLog.Model.LogSubscriptionManager.<SaveChanges>b__2() in C:\github.com\tomhunter-gh\SourceLog\SourceLog.Model\LogSubscriptionManager.cs:line 51
InnerException: (null)
Aquí está el código que llama SaveChanges()
:
internal static readonly object DbSaveLockObject = new object();
public static void SaveChanges()
{
Task.Factory.StartNew(() =>
{
lock (DbSaveLockObject)
{
Debug.WriteLine(DateTime.Now + ": SaveChanges in lock");
Db.SaveChanges();
}
});
}
¿Alguna vez resolvió el problema? Estoy teniendo un problema similar. –
Realmente no obtuve una mejor comprensión de la que tengo en mi respuesta. Puede ver el elemento que se agrega a dos colecciones en el método [AddNewLogEntry] (https://github.com/tomhunter-gh/SourceLog/blob/aed3718af18fcff471f04c83f83a0160b97b6829/SourceLog.Model/LogSubscription.cs#L90), una vez en el contexto colección y una vez a la "colección de UI". –
Tuve el mismo problema, terminó siendo un backgroundworker tratando de acceder al contexto mientras otros procesos lo estaban usando. Moví esa llamada después de que los otros procesos terminaron y eso lo resolvió. Supongo que eso también funcionaría. Por lo que sé que EF debe gestionar el enhebrado, nunca tuve un problema así hasta que haya creado la llamada de fondo. – Hannish