c# - 手动打开到 SQLite 数据库的 EntityConnection 会导致 ProviderIncompatibleException

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

出于集成测试目的,我想在测试夹具设置中手动创建并打开 EntityConnection。调用 Open() 方法时失败并出现以下异常:

System.Data.ProviderIncompatibleException:在类型为“System.Data.SQLite.EF6.SQLiteProviderFactory”的商店提供程序实例上调用“GetService”方法后返回 null。商店提供商可能无法正常运行。

我使用的是与 EF 处理连接打开时使用的相同的连接字符串。如果我运行相同的测试套件并由 EF 自动处理连接,它就可以工作。

...
[TestFixtureSetUp]
public void FixtureSetUp()
{
    // Setup database 
    // Setup data access
    ... 
    var ec = new EntityConnection([ConnectionString]);
    ec.StoreConnection.Open(); --> WORKS!!
    ec.Open(); -> Throws
}
...

连接字符串如下所示:

metadata=res://*/Test.TestModel.csdl|res://*/Test.TestModel.ssdl|res://*/Test.TestModel.msl;provider=System.Data.SQLite;provider connection string="data source=C:\Test\tmp4C80.tmp;read only=False;pooling=False;failifmissing=True;synchronous=Full;datetimekind=Utc;enlist=True;setdefaults=False;datetimeformat=ISO8601;journal mode=Off;cache size=4194304"

NUnit 程序集的 app.config 如下

<?xml version="1.0" encoding="utf-8"?>

<configuration>
    <configSections>
        <section name="entityFramework"
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                 requirePermission="false" />
    </configSections>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="System.Data.SqlClient"
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SQLite"
                      type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
        </providers>
    </entityFramework>
    <!-- Register protable database data providers -->
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite" />
            <add name="SQLite Data Provider" invariant="System.Data.SQLite"
                 description=".Net Framework Data Provider for SQLite"
                 type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
        </DbProviderFactories>
    </system.data>
</configuration>

除了用于测试的 NUnit,我还使用 Entity Framework 6.1.1 和 System.Data.SQLite 1.0.94.0。

编辑:奇怪的是,手动打开提供的实体连接的商店连接是有效的...

最佳答案

我发现我使用了 System.Data.Entity 中的 EntityConnection 而不是 EntityFramework 程序集。

关于c# - 手动打开到 SQLite 数据库的 EntityConnection 会导致 ProviderIncompatibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26144046/

相关文章:

c# - 如何在 C++/CLI 中包装 C++ 接口(interface)(抽象类)?

android - 解析和存储电子书内容,sqlite 索引

c# - App挂起后恢复相机资源

c# - 在施加力之前绘制射弹的点

c# - JsonPatchDocument 到复杂的 Entity Framework 跟踪对象

c# - 使用 Moq 更新、删除单元测试的方法

android - Android中对SQLite数据库的多次访问

android SQLiteDatabase.Update() 返回 2,为什么?

c# - 如何忽略秒 ToShortTimeString

c# - EF DataContractSerializer 异常