c# - 在 .NET 4.0 基于代码的配置中使用 SQLite 的 EF6 : "Unable to Determine the DbProviderFactory"

标签 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>

在 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)));
    }
}

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

Unable to determine the DbProviderFactory type for connection of type 'System.Data.SQLite.SQLiteConnection'. Make sure that the ADO.NET provider is installed or registered in the application config.

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)

我试过:

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

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

此外,SQLiteProviderService 类是内部的,根据this answer,唯一的获取方式是通过反射。 .程序真的可以访问服务吗?不过,这似乎对大多数人都有效。

非常感谢任何帮助或想法来尝试解决这个问题!


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

最佳答案

Here我发现了帮助我以编程方式使用 EF6 配置 sqlite 的建议。

该答案的代码块:

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

if (dataRow != null)
    dbProviderFactoriesDataTable.Rows.Remove(dataRow);

相当于:

<remove invariant="" />

关于c# - 在 .NET 4.0 基于代码的配置中使用 SQLite 的 EF6 : "Unable to Determine the DbProviderFactory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38501836/

相关文章:

c# - 使用 ExpressionVisitor 排除连接中的软删除记录

c# - 使用负载均衡器的 ASP.NET 应用程序生命周期

c# - 最佳实践 LongRunning 任务创建

c# - Visual Studio Code launchurl 不适用于 Web API

c# - 如何在 HTML head 中查找和删除 CSS 引用?

c# - 返回带有 Ajax 的 PartialView 未在页面上插入

algorithm - 从 SHA-512 哈希生成的字符不会保存到数据库中

c# - EF 代码优先 : Sequence contains more than one matching element

c# - 从 ID 的 C# 列表中删除 mysql 服务器中的行

c# - 如何从 xamarin.forms 调用平台特定页面