代码改变世界

让EntityFramework6支持SQLite

2014-08-29 10:21  埋头前进的码农  阅读(14804)  评论(26编辑  收藏  举报

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

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

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

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

Install-Package System.Data.SQLite.EF6
Install-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" />
   
  </configSections>
  <connectionStrings>
    <add name="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=education.db;Pooling=True" />
  </connectionStrings>
<entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </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" />
    </DbProviderFactories>
  </system.data>

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

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

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

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

<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了,但是,又会抛如下异常:

unable to open database file

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

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

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

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

SQL logic error or missing database
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