加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:00:46 所属栏目:百科 来源:网络整理
导读:我试图让EF6基于代码的配置与.NET 4.0中的SQLite一起使用.在我的目标应用程序中,App.config是自动生成的,因此编辑非常痛苦.最初通过NuGet将SQL6安装到SQLite到我的测试应用程序时,它会生成以下配置: configuration configSections !-- For more information
我试图让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="" />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读