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

让EntityFramework6支持SQLite

发布时间:2020-12-12 19:21:50 所属栏目:百科 来源:网络整理
导读:原文地址:http://www.cnblogs.com/lifeil/p/3944334.html 最近给朋友的小孩做了一个毕业设计。用的是asp.netMVC5 +EntityFramework6 +SQLServer 2008. 结果做好后,朋友说能不能不要数据库,直接运行?顿时让我很纠结,不需要安装数据库但又能运行的,只能考
1 2 Install-Package System.Data.SQLite.EF6Install-Package System.Data.SQLite.Linq

安装后会出现三个引用:


接着Web.config中配置如下:

<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" />
   
  </>
  connectionStringsadd ="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=education.db;Pooling=True" />
  >
entityFrameworkproviders>
      provider invariantName="System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6" />
    defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"parameters>
        parameter value="v11.0" />
      defaultConnectionFactorysystem.dataDbProviderFactoriesremove invariant="System.Data.SQLite.EF6" ="SQLite Data Provider (Entity Framework 6)" invariant description=".Net Framework Data Provider for SQLite (Entity Framework 6)"="System.Data.SQLite.EF6.SQLiteProviderFactory,255); line-height:1.5!important">>

这样配置后,发现会抛异常信息如下:

1 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的配置。

在entityFramework节点的providers子节点添加配置如下:

<providerinvariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices,System.Data.SQLite.EF6" />

接着在system.data节点的DbProviderFactories子节点配置如下:

addname="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory,System.Data.SQLite"/>

这下终于支持ado.net了,但是,又会抛如下异常:

unable to open database file

大概是无法打开数据库文件,经查资料发现,程序读取SQLite数据库时需要使用物理绝对路径,解决方法有两个,一个是在代码中指定数据库配置文件,一个是在Web.config中指定一个类似变量的值,如下:

="Data Source=|DataDirectory|education.db;Pooling=True" />

其中DataDirectory指的是数据库的目录,对应着的是asp.net项目下的App_Data文件夹,所以,需要把数据库放到该目录。

修改完毕后又会抛另外一个异常:

SQL logic error or missing databaseno such table: Articles

大概是说没有找到表Articles,我的项目用的是CodeFirst模式,看来SQLite不支持CodeFirst模式,只能手动建表了。如果表比较少或是字段比较少还行,如果有大量的表,光手动建表也得费好大事,如果能够把SQLServer 2008的数据库导入到SQLite中就好了。

经谷歌后,终于找到一个工具SqlConverter,该工具能够将SQLServer的表和表内的数据库转换成SQLite。

经转换后发现,SQLite中的主键只能是integer类型的,对应C#的int64。而我的Model却是int32,不知道是SQLite规定的还是工具转换有问题。

完整的Web.config配置如下:

1<?xml version="1.0" encoding="utf-8"?> 2<!-- 3 有关如何配置 ASP.NET 应用程序的详细信息,请访问 4 http://go.microsoft.com/fwlink/?LinkId=301880 5--> 6configuration 7 8 9/>101112appSettings13key="webpages:Version" value="3.0.0.0"14="webpages:Enabled"15="ClientValidationEnabled"="true"16="UnobtrusiveJavaScriptEnabled"171819<add name="EducationStrings" providerName="System.Data.SqlClient" connectionString="Data Source=.; User=sa;Password=123456;Initial Catalog=EducationDb;Integrated Security=True" />202122system.web23compilation debug="true" targetFramework="4.5"24httpRuntime targetFramework2526system.webServer27validation validateIntegratedModeConfiguration28modules runAllManagedModulesForAllRequests="true"29<add name="AuthorizeModule" type="Education.Web.AuthorizeModule"/>30modules313233runtime34assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"35dependentAssembly36assemblyIdentity ="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"37bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"383940="System.Web.Optimization"="31bf3856ad364e35"41="1.0.0.0-1.1.0.0"="1.1.0.0"424344="WebGrease"45="0.0.0.0-1.5.2.14234"="1.5.2.14234"464748="System.Web.Helpers"49="1.0.0.0-3.0.0.0"505152="System.Web.WebPages"53="0.0.0.0-3.0.0.0"545556="System.Web.Mvc"57="0.0.0.0-5.2.0.0"="5.2.0.0"5859assemblyBinding60616263="System.Data.SQLite"646566676869707172737475="SQLite Data Provider"=".Net Framework Data Provider for SQLite"="System.Data.SQLite.SQLiteFactory,System.Data.SQLite"76777879 这样终于可以运行了。附代码和工具。

代码下载:

http://download.csdn.net/detail/lifeilin6671/7837447

转换工具下载:

http://download.csdn.net/detail/lifeilin6671/7837465

(编辑:李大同)

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

原文地址:http://www.cnblogs.com/lifeil/p/3944334.html

最近给朋友的小孩做了一个毕业设计。用的是asp.netMVC5 +EntityFramework6 +SQLServer 2008.

结果做好后,朋友说能不能不要数据库,直接运行?顿时让我很纠结,不需要安装数据库但又能运行的,只能考虑单文件的数据库了,比如说Access或是SQLite。

查阅资料后发现Access无法支持EntityFramework的运行,只要考虑SQLite。

经查阅资料发现,SQLite方法发布了对EntityFramework6支持的程序集,在项目中执行如下命令:

    推荐文章
      热点阅读