讓EntityFramework6支持SQLite


最近給朋友的小孩做了一個畢業設計。用的是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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM