基于 .NET 4.0 代码的配置中带有 SQLite 的 EF6:“无法确定 DbProviderFactory”

Sil*_*fir 6 c# .net-4.0 ef-code-first entity-framework-6

我正在尝试让 EF6 基于代码的配置与 .NET 4.0 中的 SQLite 一起使用。在我的目标应用程序中App.config是自动生成的,因此编辑非常痛苦。最初,当通过 NuGet 将 EF6 安装到 SQLite 到我的测试应用程序时,它会生成以下有效配置

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)

在 SQL Server CE 上,我能够将 的内容复制App.config到 DbConfiguration 类。同样我试过:

class DBConfig : DbConfiguration
{
    public DBConfig() : base()
    {
        this.SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));

        this.SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
        this.SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);

        this.SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
        this.SetProviderServices("System.Data.SQLite.EF6", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当通过给构造函数 SQLiteConnection 或 connectionString 创建上下文时,这会产生 System.NotSupportedException:

无法确定类型为“System.Data.SQLite.SQLiteConnection”的连接的 DbProviderFactory 类型。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。

at System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver.<>c__DisplayClass5.<GetProviderFactory>b__0(Type t)
at System.Collections.Concurrent.ConcurrentDictionary'2.GetOrAdd(TKey key, Func'2 valueFactory)
at System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver.GetProviderFactory(DbConnection connection, IEnumerable'1 dataRows)
at System.Data.Entity.Infrastructure.Net40DefaultDbProviderFactoryResolver.ResolveProviderFactory(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderFactory(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderFactory(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection)
at System.Data.Entity.Internal.InternalConnection.get_ProviderName()
at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName()
at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet'1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet'1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet'1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet'1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
Run Code Online (Sandbox Code Playgroud)

我试过了:

  • SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    根据这个答案添加。
  • AddDependencyResolver(new SingletonDependencyResolver<DbProviderFactory>(SQLiteProviderFactory.Instance));
    为 DbProviderServices 和 IDbConnectionFactory添加和 DependencyResolvers,甚至尝试将它们设置为 DefaultResolvers。
  • 创建我自己的 SQLiteConnectionFactory 并根据此答案将其设置为默认值。

我记得在某处读到,如果文件中有一个<System.Data>部分,Machine.config它可能会导致 ProviderFactories 中的混淆。是否有任何基于代码的配置等效于<remove invariant="" />

SQLiteProviderService 类也是内部的,唯一的获取方法是根据这个答案通过反射。该程序是否真的能够访问该服务?不过,这似乎对大多数人都有效。

任何尝试解决此问题的帮助或想法都非常感谢!


一个奇怪的注意事项:当从SQLiteProviderFactory.Instance.CreateConnection()连接的 DbProviderFactory 属性创建 SQLiteConnection 时,System.Data.SQLite.SQLiteFactory但工作App.config配置System.Data.Entity.Core.Common.DbProviderServices.GetProviderFactory(connection)返回SQLiteProviderFactory. 而对于基于代码的配置,它会抛出。

小智 0

在这里,我发现了一些建议,可以帮助我以编程方式使用 EF6 配置 sqlite。

该答案的代码块:

var dataRow = dbProviderFactoriesDataTable.Rows.OfType<DataRow>()
                    .FirstOrDefault(x => x.ItemArray[2].ToString() == assemblyName);

if (dataRow != null)
    dbProviderFactoriesDataTable.Rows.Remove(dataRow);
Run Code Online (Sandbox Code Playgroud)

相当于:

<remove invariant="" />
Run Code Online (Sandbox Code Playgroud)