目录
1.1、缺乏System.Data.SQLite.EF6工厂提供者
注意:
- 以下问题,要大于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="data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3"" 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="data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3"" 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="data source=D:\keenray6.2\trunk\Bin\Database\iPadDataBase.db3"" 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