在VS2013环境下使用EF框架与Sqlite(一)
来源:互联网 发布:苹果公司营销策略数据 编辑:程序博客网 时间:2024/06/08 15:57
在VS开发+EF框架的环境下使用Sqlite数据库是一个比较繁琐的事情, 因为Sqlite针对不同版本的VS, 有着不同版本的vs环境安装插件,如果不太懂的话,很容易装错版本导致在建立Model的时候无法看到数据库连接.在此就将具体的连接步骤在这里详细说明一下.
一、下载并安装SQLite的VS插件
登录网站 https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ,下载属于你自己IDE版本的Sqlite 的VS插件.这里需要注意两点: 一是 列表里秘密麻麻给了好多插件下载包,实际上, 只有下载描述带有“32-bit”(不管你的实际电脑环境是64还是32都要选这个),文件名带“ bundle”,才是能给VS安装SQLite插件的, 第二点就是安装包一定要符合自己的FrameWork版本.
比如我自己的windows版本是64位,开发环境是VS2013, framework版本是 4.5 , 那么我需要的插件版本就是:
直接下载,点击下一步安装即可.
需要注意的是,一定要在这个网站上进行最新插件的下载, 其他博客给出的下载包有的都是过时的了,安上了可能会不好用.这点一定要注意.
二、项目代码里引用EF和SQLite的dll文件
右键项目,点击"管理Nuget"程序包,将下图的前五个安装包搜索并下载即可(我没特意下载过第六个,我也不知道它是怎么来的)
三、对App.Config文件进行更改(敲三遍小黑板,这里很重要!!!)
如果将上面步骤都搞定,理论上已经把EF和SQLite的相关环境搭建完毕.但实际上还有个地方需要做更改,否则EF还是无法正确执行,会报出如下的错误:
Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
大概是说没有找个支持ado.net的管道工厂方法。在stackoverflow搜索发现,需要在Web.config中添加对System.Data.SQLite.SQLiteFactory的配置。
其实原因很简单: 在Nuget下载完上面5个安装包后, 会自动改写程序所在的App.Config文件. 但有个两个点该写的不符合EF环境的引用情况.所以需要我们手工再增加一些配置.
打开App.Config文件,按照如下节点进行查找
configuration→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" />这两行配置其实是错误的,并没有真正引用到我们需要的dll, 所以我们放着这两行不去管它,再增加如下两行记录
<!--这两个属性是需要后添加的,否则EF会报错 --> <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" />整个App.Config全部展示如下:
<?xml version="1.0" encoding="utf-8"?><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.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </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> <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" /> <!--这两个属性是需要后添加的,否则EF会报错 --> <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> <connectionStrings> <add name="testEntities1" connectionString="metadata=res://*/LocalModel.csdl|res://*/LocalModel.ssdl|res://*/LocalModel.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=C:\Users\Administrator\Desktop\下载目录\SQLiteSpy_1.9.8\test.db"" providerName="System.Data.EntityClient" /> </connectionStrings></configuration>
这样的,整个EF和SQLite的框架所需的基础引用方面就搞定了, 下一篇文章将介绍如何进行EF项目的建立.
自己解决问题的时候参考了 李飞麟的博客 ,特表感谢!
- 在VS2013环境下使用EF框架与Sqlite(一)
- 在VS2013环境下使用EF框架与Sqlite(二)
- Vs2013集成EF和SQLite的步骤(一)
- VS2010+SQlite+EF 一、环境搭建
- 在VC/VS环境下使用Sqlite
- SQLite在VC环境下的使用
- 如何在VS2013+QT5.3环境下使用自定义信号与槽函数
- QT5.5与VS2013在64位环境下集成
- iocomp控件-iPlotX在vs2013 mfc环境下的使用
- VS2013环境下使用FreeType2
- Windows10+VS2013环境下Dlib库的编译与使用
- sqlite使用总结(一)搭建编译与使用环境
- FFTW3在VS2013下的安装与使用示例
- WinPacp在VS2013环境下的配置
- WinPacp在VS2013环境下的配置
- opencv在vs2013下的环境搭建
- Vs2013集成EF和SQLite的步骤(二)
- VS2013下使用创建sqlite的ADO.NET实体数据模型
- hdu 6090-Rikka with Graph
- 关于77页读写string对象编程错误
- 解决IE8 内置JSON.stringify,中文变unicode的问题
- 营销思维系列——1
- springboot之过滤器,监听器的使用
- 在VS2013环境下使用EF框架与Sqlite(一)
- Hadoop-No.15之Flume基于事件的数据收集和处理
- 通过 SQL 查询学习 Pandas 数据处理
- Android studio sdk tools 无法更新 cmake和LLDB方法
- Java-Java8
- Android 如何连续的测量蓝牙的RSSI
- Latex note
- 如何理解style中display:inline-block
- SYD8801 4K_setting文件说明【时钟源的选择】