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

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

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

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

  1. Install-Package System.Data.SQLite.EF6
  2. Install-Package System.Data.SQLite.Linq

安装后会出现三个引用:

接着Web.config中配置如下:

  1. <configSections>
  2. <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  3. <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  4.  
  5. </configSections>
  6. <connectionStrings>
  7. <add name="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=education.db;Pooling=True" />
  8. </connectionStrings>
  9. <entityFramework>
  10. <providers>
  11. <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
  12. </providers>
  13. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  14. <parameters>
  15. <parameter value="v11.0" />
  16. </parameters>
  17. </defaultConnectionFactory>
  18. </entityFramework>
  19. <system.data>
  20. <DbProviderFactories>
  21. <remove invariant="System.Data.SQLite.EF6" />
  22. <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" />
  23. </DbProviderFactories>
  24. </system.data>

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

  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子节点添加配置如下:

  1. <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

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

  1. <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

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

  1. unable to open database file

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

  1. <add name="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=|DataDirectory|\education.db;Pooling=True" />

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

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

  1. SQL logic error or missing database
  2. no such table: Articles

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

经谷歌后,终于找到一个工具SqlConverter,该工具能够将SQL Server的表和表内的数据库转换成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. -->
  6. <configuration>
  7. <configSections>
  8. <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  9. <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  10.  
  11. </configSections>
  12. <appSettings>
  13. <add key="webpages:Version" value="3.0.0.0" />
  14. <add key="webpages:Enabled" value="false" />
  15. <add key="ClientValidationEnabled" value="true" />
  16. <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  17. </appSettings>
  18. <connectionStrings>
  19. <!--<add name="EducationStrings" providerName="System.Data.SqlClient" connectionString="Data Source=.; User=sa;Password=123456;Initial Catalog=EducationDb;Integrated Security=True" />-->
  20. <add name="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=|DataDirectory|\education.db;Pooling=True" />
  21. </connectionStrings>
  22. <system.web>
  23. <compilation debug="true" targetFramework="4.5" />
  24. <httpRuntime targetFramework="4.5" />
  25. </system.web>
  26. <system.webServer>
  27. <validation validateIntegratedModeConfiguration="false" />
  28. <modules runAllManagedModulesForAllRequests="true">
  29. <!--<add name="AuthorizeModule" type="Education.Web.AuthorizeModule"/>-->
  30. </modules>
  31. </system.webServer>
  32.  
  33. <runtime>
  34. <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  35. <dependentAssembly>
  36. <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
  37. <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  38. </dependentAssembly>
  39. <dependentAssembly>
  40. <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
  41. <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
  42. </dependentAssembly>
  43. <dependentAssembly>
  44. <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
  45. <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  46. </dependentAssembly>
  47. <dependentAssembly>
  48. <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
  49. <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  50. </dependentAssembly>
  51. <dependentAssembly>
  52. <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
  53. <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  54. </dependentAssembly>
  55. <dependentAssembly>
  56. <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
  57. <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
  58. </dependentAssembly>
  59. </assemblyBinding>
  60. </runtime>
  61. <entityFramework>
  62. <providers>
  63. <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
  64. <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
  65. </providers>
  66. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  67. <parameters>
  68. <parameter value="v11.0" />
  69. </parameters>
  70. </defaultConnectionFactory>
  71. </entityFramework>
  72. <system.data>
  73. <DbProviderFactories>
  74. <remove invariant="System.Data.SQLite.EF6" />
  75. <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
  76. <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" />
  77. </DbProviderFactories>
  78. </system.data>
  79. </configuration>

这样终于可以运行了。附代码和工具。

代码下载:

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

转换工具下载:

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

让EntityFramework6支持SQLite的更多相关文章

  1. [开源].NET高性能框架Chloe.ORM-完美支持SQLite

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

  2. 让 PowerDesigner 支持 SQLite!

    让 PowerDesigner 支持 SQLite!   PowerDesigner是一个功能强大的数据库设计软件,最近正在用其设计新系统的数据库,但由于在项目初级阶段,希望使用轻量级的 SQLite ...

  3. DataUml Design 介绍8-DataUML 1.2版本正式发布(支持SQLite数据库、NetUML开发框架)

    DataUML 1.2版本在软件架构上有了很大的变化,目前DataUML支持Access.SQLite.MY SQL .ORACLE.MS SERVER2000.MS SERVER2005.MS SE ...

  4. SkylineGlobe7.0.1版本 支持SQLite(*.sqlite;*.db)数据库

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  5. 【C#】使用EF访问Sqlite数据库

    原文:[C#]使用EF访问Sqlite数据库 1. 先上Nuget下载对应的包 如图,搜索System.Data.SQLite下载安装即可,下载完之后带上依赖一共有这么几个: EntityFramew ...

  6. 如何在 Ubuntu 15.04 上安装带 JSON 支持的 SQLite 3.9

    欢迎阅读我们关于SQLite 的文章,SQLite 是当今世界上使用最广泛的 SQL 数据库引擎,它基本不需要配置,不需要设置或管理就可以运行.SQLite 是一个是公开领域(public-domai ...

  7. QT实现支持加密的Sqlite数据库引擎

    Sqlite数据库使用很广泛,我们经常会在发布一些小型软件的时候使用它,因为它不需要安装服务器.QT默认的数据库引擎是支持SQLITE数据库的,但并不支持对数据库加密,不加密的Sqlite数据库任何人 ...

  8. 【Win 10 应用开发】Sqlite 数据库的简单用法

    如果老周没记错的话,园子里曾经有朋友写过如何在 UWP 项目中使用 Sqlite数据库的文章.目前我们都是使用第三方封装的库,将来,SDK会加入对 Sqlite 的支持. 尽管目前 UWP-RT 库中 ...

  9. [开源].NET高性能框架Chloe.ORM-完美支持.NET Core

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

随机推荐

  1. JavaScript中的继承模式总结

    一.总结: //js中的几种继承 //原型链的问题,包含引用类型的原型属性会被实例共享,子类型无法给超类型传递参数 function SuperType() { this.colors = [&quo ...

  2. 剑指offer系列58---把数组排成最小的数

    [题目]输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个. * 例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.[思路]1 ...

  3. bzoj4709 [jsoi2011]柠檬

    Description Flute 很喜欢柠檬.它准备了一串用树枝串起来的贝壳,打算用一种魔法把贝壳变成柠檬.贝壳一共有 N (1 ≤ N  ≤ 100,000) 只,按顺序串在树枝上.为了方便,我们 ...

  4. php反射机制获取未知类的详细信息

    使用ReflectionClass就可以获取未知类的详细信息 demo: require("hello.php"); $class = new ReflectionClass(&q ...

  5. golang的验证码相关的库

    识别库 https://github.com/goghcrow/capture_easy 生成验证码的库 https://github.com/hanguofeng/gocaptcha 生成图片水印 ...

  6. Node.js 相关资料网站汇总

    地址:https://cnodejs.org/ nodejs中文网:http://nodejs.cn/ nodejs中文网:http://www.nodejs.net/ 相关API地址:http:// ...

  7. c#处理3种json数据的实例

    网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...

  8. Form_Form Builder Export导出为Excel(案例)

    2014-01-09 Created By BaoXinjian  

  9. CE_现金银行总行分行设定详解(案例)

    2014-07-14 Created By BaoXinjian

  10. Python分布式爬虫原理

    转载 permike 原文 Python分布式爬虫原理 首先,我们先来看看,如果是人正常的行为,是如何获取网页内容的. (1)打开浏览器,输入URL,打开源网页 (2)选取我们想要的内容,包括标题,作 ...