【问题标题】:Updating to EF 6.2.0 from EF 6.1.3 causes cannot access a disposed object error从 EF 6.1.3 更新到 EF 6.2.0 导致无法访问已处置对象错误
【发布时间】:2018-04-29 23:33:57
【问题描述】:

我正在使用 SQLite。我可以在我的 WPF 应用程序中毫无问题地使用实体框架 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"...

有什么想法吗?

【问题讨论】:

  • 请向我们展示CRUD_On_PipeGetAll的源代码。
  • @mjwills 是的,我确实更新了应用程序和测试项目。
  • @mjwills 用这些源代码更新了问题。
  • 需要查看DatabaseContext.cs:line 114
  • @DanielLorenz 请查看更新后的问题。

标签: c# entity-framework-6 system.data.sqlite


【解决方案1】:

问题是由 RepositoryBase 类的更改和 SQLiteConnection 类对 IDbConnection.State 属性的错误 (IMO) 实现引起的(在处置对象上调用时抛出 ObjectDisposedException 而不是返回 ConnectionState.Closed)。

#398: NullReferenceException on Code First Migrations when Glimpse is installed 中报告的相同。根据状态,已经在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);
    }
}

原来的异常已经消失,但现在我又收到了一个提示 'No MigrationSqlGenerator found for provider 'System.Data.SQLite'。使用目标迁移配置类中的 SetSqlGenerator 方法来注册其他 SQL 生成器。' 我认为这是与 SQLite EF 服务中缺少 MigrationSqlGenerator 相关的另一个问题。这可能是也可能不是问题,具体取决于您在 6.1.3 中是如何解决的。

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

【讨论】:

    【解决方案2】:

    修复了同样影响 GlimpseDB 的问题:https://github.com/aspnet/EntityFramework6/pull/405

    现在等待发布。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2011-10-23
      • 2011-03-25
      相关资源
      最近更新 更多