SQL Server让EntityFramework陆帮助SQLite

近期给心上人的孩子做了二个结束学业设计。用的是asp.net MVC5 + EntityFramework6+ SQL Server 二零零六.

结果做好后,朋友说能否不要数据库,间接运营?霎时让本人很纠结,不须求设置数据库但又能运作的,只好怀恋单文件的数据库了,比如说Access或是SQLite。

翻看资料后意识Access不可能帮助EntityFramework的运维,只要考虑SQLite。

经查看资料发现,SQLite方法发表了对EntityFramework六支撑的程序集,在项目中实践如下命令:

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

安装后会出现多少个引用:

SQL Server 1 

随着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数据库时必要动用物理相对路径,化解格局有四个,2个是在代码中钦点数据库配置文件,2个是在Web.config中钦点一个类似变量的值,如下:

<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
no such table: Articles

 大致是说未有找到表Articles,作者的品种用的是CodeFirst格局,看来SQLite不帮忙CodeFirst形式,只可以手动建表了。假若表相比较少或是字段比较少还不错,借使有大气的表,光手动建表也得费好大事,倘若能够把SQL Server
二零零六的数据库导入到SQLite中就好了。

经谷歌后,终于找到二个工具SqlConverter,该工具能够将SQL Server的表和表内的数据库转换来SQLite。

经转换后发觉,SQLite中的主键只好是integer类型的,对应C#的int6四。而自作者的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

 

相关文章