ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

c# – 在.NET 4.0中使用SQLite的EF6基于代码的配置:“无法确定DbProviderFactory”

2019-07-11 08:04:34  阅读:242  来源: 互联网

标签:c ef-code-first net-4-0 entity-framework-6


我试图让EF6基于代码的配置与.NET 4.0中的SQLite一起使用.在我的目标应用程序中,App.config是自动生成的,因此编辑非常痛苦.最初通过NuGet将SQL6安装到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来创建Context时,这会产生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));和DbProviderServices和IDbConnectionFactory的DependencyResolvers甚至尝试将它们设置为DefaultResolvers.
>根据this answer创建我自己的SQLiteConnectionFactory并将其设置为默认值.

我记得在某处读过如果有< 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(连接)返回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,ef-code-first,net-4-0,entity-framework-6
来源: https://codeday.me/bug/20190711/1430312.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有