標籤:des blog http os 使用 java io ar for
最近給朋友的小孩做了一個畢業設計。用的是asp.net MVC5 + EntityFramework6 + SQL Server 2008.
結果做好後,朋友說能不能不要資料庫,直接運行?頓時讓我很糾結,不需要安裝資料庫但又能啟動並執行,只能考慮單檔案的資料庫了,比如說Access或是SQLite。
查閱資料後發現Access無法支援EntityFramework的運行,只要考慮SQLite。
經查閱資料發現,SQLite方法發布了對EntityFramework6支援的程式集,在項目中執行如下命令:
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" /> </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的管道Factory 方法。在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 databaseno such table: Articles
大概是說沒有找到表Articles,我的項目用的是CodeFirst模式,看來SQLite不支援CodeFirst模式,只能手動建表了。如果表比較少或是欄位比較少還行,如果有大量的表,光手動建表也得費好大事,如果能夠把SQL Server 2008的資料庫匯入到SQLite中就好了。
經Google後,終於找到一個工具SqlConverter,該工具能夠將SQL Server的表和表內的資料庫轉換成SQLite。
經轉換後發現,SQLite中的主鍵只能是integer類型的,對應C#的int64。而我的Model卻是int32,不知道是SQLite規定的還是工具轉換有問題。
完整的Web.config配置如下:
<?xml version="1.0" encoding="utf-8"?><!-- 有關如何配置 ASP.NET 應用程式的詳細資料,請訪問 http://go.microsoft.com/fwlink/?LinkId=301880 --><configuration> <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> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <connectionStrings> <!--<add name="EducationStrings" providerName="System.Data.SqlClient" connectionString="Data Source=.; User=sa;Password=123456;Initial Catalog=EducationDb;Integrated Security=True" />--> <add name="EducationStrings" providerName="System.Data.SQLite.EF6" connectionString="Data Source=|DataDirectory|\education.db;Pooling=True" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true"> <!--<add name="AuthorizeModule" type="Education.Web.AuthorizeModule"/>--> </modules> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" /> </dependentAssembly> </assemblyBinding> </runtime><entityFramework> <providers> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> <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" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/> <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></configuration>
這樣終於可以運行了。附代碼和工具。
代碼下載:
http://download.csdn.net/detail/lifeilin6671/7837447
轉換工具下載:
http://download.csdn.net/detail/lifeilin6671/7837465
讓EntityFramework6支援SQLite