讓EntityFramework6支持SQLite

最近給朋友的小孩作了一個畢業設計。用的是asp.net MVC5 + EntityFramework6 + SQL Server 2008.html

結果作好後,朋友說能不能不要數據庫,直接運行?頓時讓我很糾結,不須要安裝數據庫但又能運行的,只能考慮單文件的數據庫了,好比說Access或是SQLite。web

查閱資料後發現Access沒法支持EntityFramework的運行,只要考慮SQLite。數據庫

經查閱資料發現,SQLite方法發佈了對EntityFramework6支持的程序集,在項目中執行以下命令:app

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

安裝後會出現三個引用:asp.net

 

接着Web.config中配置以下:ide

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

在entityFramework節點的providers子節點添加配置以下:spa

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

 接着在system.data節點的DbProviderFactories子節點配置以下:.net

<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

相關文章
相關標籤/搜索