C# 从EF 6.1.3更新到EF 6.2.0导致无法访问已处理的对象错误

C# 从EF 6.1.3更新到EF 6.2.0导致无法访问已处理的对象错误,c#,entity-framework-6,system.data.sqlite,C#,Entity Framework 6,System.data.sqlite,我正在与SQLite合作。我能够在我的WPF应用程序中使用entity framework 6.1.3,没有问题,但是当我将其更新到6.2.0时,我得到以下错误: Test method DataAccessLayerTests.GenericDataRepositoryTests.CRUD_On_Pipe threw exception: System.ObjectDisposedException: Cannot access a disposed object. Object name:

我正在与SQLite合作。我能够在我的WPF应用程序中使用entity framework 6.1.3,没有问题,但是当我将其更新到6.2.0时,我得到以下错误:

Test method DataAccessLayerTests.GenericDataRepositoryTests.CRUD_On_Pipe threw exception: 
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SQLiteConnection'.
    at System.Data.SQLite.SQLiteConnection.CheckDisposed()
   at System.Data.SQLite.SQLiteConnection.get_State()
   at System.Data.Entity.Internal.RepositoryBase.CreateConnection()
   at System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(String contextKey)
   at System.Data.Entity.Migrations.History.HistoryRepository.Exists(String contextKey)
   at System.Data.Entity.Migrations.History.HistoryRepository.GetPendingMigrations(IEnumerable`1 localMigrations)
   at System.Data.Entity.Migrations.DbMigrator.GetPendingMigrations()
   at Core.DatabaseContext.CreateAndSeedIfNotExists`1.InitializeDatabase(T context) in C:\Users\roadrunner\propulsimcs\Propulsim\Core\DatabaseContext.cs:line 40
   at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
   at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Database.Initialize(Boolean force)
   at Core.DatabaseContext..ctor() in C:\Users\roadrunner\propulsimcs\Propulsim\Core\DatabaseContext.cs:line 114
   at DataAccessLayer.GenericDataRepository`1.GetAll(Expression`1[] navigationProperties) in C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayer\GenericDataRepository.cs:line 16
   at DataAccessLayerTests.GenericDataRepositoryTests.CRUD_On_Pipe() in C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayerTests\GenericDataRepositoryTests.cs:line 34




Debug Trace:
Native library pre-loader is trying to load native SQLite library "C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayerTests\bin\Debug\x86\SQLite.Interop.dll"...
测试方法DataAccessLayerTests.GenericDataRepositoryTests.CRUD_On_管道引发异常:
System.ObjectDisposedException:无法访问已处置的对象。
对象名称:“SQLiteConnection”。
在System.Data.SQLite.SQLiteConnection.CheckDisposed()处
在System.Data.SQLite.SQLiteConnection.get_State()中
位于System.Data.Entity.Internal.RepositoryBase.CreateConnection()处
位于System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(字符串contextKey)
位于System.Data.Entity.Migrations.History.HistoryRepository.Exists(字符串contextKey)
位于System.Data.Entity.Migrations.History.HistoryRepository.GetPendingMigrations(IEnumerable`1 localMigrations)
位于System.Data.Entity.Migrations.DbMigrator.GetPendingMigrations()处
在C:\Users\roadrunner\propulsimcs\Propulsim\Core\DatabaseContext.cs:第40行中的Core.DatabaseContext.CreateandSeedIFnoteExists`1.InitializeDatabase(T上下文)
在System.Data.Entity.Internal.InternalContext.c__DisplayClassf`1.b__e()中
位于System.Data.Entity.Internal.InternalContext.PerforminizationAction(操作操作)
位于System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()处
位于System.Data.Entity.Internal.LazyInternalContext.b_uu4(InternalContext c)
在System.Data.Entity.Internal.RetryAction`1.PerformAction(输入)
位于System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 Action)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()中
位于System.Data.Entity.Internal.InternalContext.Initialize()处
在System.Data.Entity.Database.Initialize处(布尔强制)
在C:\Users\roadrunner\propulsimcs\Propulsim\Core\DatabaseContext.cs中的Core.DatabaseContext..ctor()处:第114行
位于C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayer\GenericDataRepository.cs中的DataAccessLayer.GenericDataRepository`1.GetAll(表达式`1[]navigationProperties):第16行
在C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayerTests\GenericDataRepositoryTests.CRUD\On\u管道()中的DataAccessLayerTests.GenericDataRepositoryTests.cs:第34行
调试跟踪:
本机库预加载程序正在尝试加载本机SQLite库“C:\Users\roadrunner\propulsimcs\Propulsim\DataAccessLayerTests\bin\Debug\x86\SQLite.Interop.dll”。。。

有什么想法吗?

这个问题是由
RepositoryBase
类中的更改和
SQLiteConnection
类对属性的错误(IMO)实现引起的(调用disposed object时抛出
ObjectDisposedException
而不是返回
ConnectionState.Closed

这与报告中的相同。根据状态,它已经在EF6存储库中修复,但不幸的是,他们决定不提供补丁,所以您必须等待v6.3。我已经在这篇文章中报告了SQLite问题,希望他们能改变主意

另一种选择是将问题报告给SQLite开发,然后等待修复。在这两种情况下,您都必须等待SQLite或EF6端的修复。请注意,即使使用标准的
MigrateDatabaseToLatestVersion
初始值设定项,该问题也是可以重现的

我通过使用以下丑陋的反射黑客解决了这个问题:

public override void InitializeDatabase(T context)
{
    base.InitializeDatabase(context);

    var _historyRepository = migrator.GetType().GetField("_historyRepository", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(migrator);
    var _existingConnection = _historyRepository.GetType().BaseType.GetField("_existingConnection", BindingFlags.Instance | BindingFlags.NonPublic);
    _existingConnection.SetValue(_historyRepository, null);

    var x = migrator.GetPendingMigrations();
    if (x.Any())
    {
        migrator.Update();
        Seed(context);
    }
}
最初的异常消失了,但现在我看到另一个异常,它是“没有为提供程序找到MigrationSqlGenerator”System.Data.SQLite。使用target migrations configuration类中的SetSqlGenerator方法注册其他SQL生成器。“我认为这是另一个问题,与SQLite EF服务中缺少
MigrationSqlGenerator
有关。这可能是问题,也可能不是问题,这取决于您在6.1.3中是如何解决的


无论如何,我不建议使用上述黑客。请等待fox修复或暂时降级到6.1.3。

同样影响GlimpseDB的问题已修复:


正在等待发布。

请向我们展示管道上的积垢的源代码。和
GetAll
@mjwills是的,我更新了应用程序和测试项目。@mjwills用这些源代码更新了问题。需要查看数据库上下文。cs:line114@DanielLorenz请参阅更新的问题。