SQLite基础(五)缺乏数据提供者引发异常:The ADO.NET provider with invariant name 'System.Data.SQLite.EF6'

目录

 一、异常信息

1.1、缺乏System.Data.SQLite.EF6工厂提供者

1.2、缺乏System.Data.SQLite工厂提供者

二、异常原因

三、解决办法

3.1、方法1(解决异常1.1,这种方法,我已经证实)

3.2、方法2(解决异常1.2)

四、结论

参考:



注意:

  • 以下问题,要大于VS2015版本。 因为VS2015,只能解析System.Data.SQLite 1.0.109.0的版本。
  • VS2015,加载System.Data.SQLite 1.0.109.0的版本的结果,请一定要看我的结论。
  • VS2015本身么有SQLite驱动,要自己添加。https://blog.csdn.net/qq_42063091/article/details/85266432
  • 随着VS版本升级,所支持的SQLite驱动可能会改变。所以下载SQLite驱动驱动时,一定要注意。

 一、异常信息

      配置SQLite数据库的连接,通常会遇到两种问题,分别是缺乏System.Data.SQLite.EF6、System.Data.SQLite工厂提供者。

1.1、缺乏System.Data.SQLite.EF6工厂提供者

     首先,手动添加下面的DLL方式,版本为1.0.109.0

并且 App.config配置如下:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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>
  <connectionStrings>
    <add name="iPadDataBaseEntities" connectionString="metadata=res://*/PadDbContent.PadDbContentModel.csdl|res://*/PadDbContent.PadDbContentModel.ssdl|res://*/PadDbContent.PadDbContentModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

然后,运行发现,异常信息如下: 

System.ArgumentException: The ADO.NET provider with invariant name 'System.Data.SQLite.EF6' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details. ---> System.ArgumentException: 找不到请求的 .Net Framework Data Provider。可能没有安装。

1.2、缺乏System.Data.SQLite工厂提供者

若是通过NeGut方式配置:

 App.config配置信息如下:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="iPadDataBaseEntities" connectionString="metadata=res://*/PadDbContent.PadDbContentModel.csdl|res://*/PadDbContent.PadDbContentModel.ssdl|res://*/PadDbContent.PadDbContentModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

异常信息:

The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'

registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

二、异常原因

      对于上述遇到的两个异常问题根本原因是没有正确配置数据工厂导致的。VS2015虽然可以通过NeGut配置数据库或者手动配置下,可能会缺失部分信息,导致不完全配置,就会出现异常。

三、解决办法

3.1、方法1(解决异常1.1,这种方法,我已经证实)

       其实,我手动配置System.Data.SQLite、System.Data.SQLite.SqlClient、System.Data.SQLite.EF6,这三个的数据工厂都配置了,但是还是出现了1.1的异常问题。

      于是,我手动删除了下面的DLL方式,版本为1.0.109.0:

       然后,再通过下面的方式配置:

     最后,App.config配置信息如下,就不会出现异常:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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>
  <connectionStrings>
    <add name="iPadDataBaseEntities" connectionString="metadata=res://*/PadDbContent.PadDbContentModel.csdl|res://*/PadDbContent.PadDbContentModel.ssdl|res://*/PadDbContent.PadDbContentModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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" />
      <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.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>

3.2、方法2(解决异常1.2)

    对于,1.2的异常问题。虽然通过NeGut进行了配置,但是NeGut的配置,还是不完整的,导致其出错。所以,我们需要通过下面的配置方式来解决。

   首先通过NeGut,配置System.Data.SQLite、System.Data.SQLite.SqlClient、System.Data.SQLite.EF6:

      找到的App.config代码(注意看啊,他没有这行代码 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />):

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>

    添加<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />到里面去,如下:

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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>

四、结论

         最后给出总结,在VS2015手动添加下面的DLL方式,就可以对SQLite数据库进行EF映射、增删改查了。同时,还可以解决https://blog.csdn.net/xpj8888/article/details/92768701这篇博客的遇到的问题。

       版本为1.0.109.0:

      并且App.config,一定要配置如下:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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>
  <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" />
      <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.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>

参考:

https://www.cnblogs.com/shihao316558512/p/5583077.html(重点)

https://blog.csdn.net/qq_42063091/article/details/85266432(这篇博客很完整)

https://www.cnblogs.com/JoeSnail/p/6474052.html(这篇博客很完整)

http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

https://www.cnblogs.com/liqipeng/p/4608508.html

https://q.cnblogs.com/q/60763/

https://www.cnblogs.com/Gyoung/p/4023275.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值