出于集成测试目的,我想在测试夹具设置中手动创建并打开 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/