最近给朋友的小孩做了一个毕业设计。用的是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配置如下:

 <?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的更多相关文章

  1. [开源].NET高性能框架Chloe.ORM-完美支持SQLite

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

  2. 让 PowerDesigner 支持 SQLite!

    让 PowerDesigner 支持 SQLite!   PowerDesigner是一个功能强大的数据库设计软件,最近正在用其设计新系统的数据库,但由于在项目初级阶段,希望使用轻量级的 SQLite ...

  3. DataUml Design 介绍8-DataUML 1.2版本正式发布(支持SQLite数据库、NetUML开发框架)

    DataUML 1.2版本在软件架构上有了很大的变化,目前DataUML支持Access.SQLite.MY SQL .ORACLE.MS SERVER2000.MS SERVER2005.MS SE ...

  4. SkylineGlobe7.0.1版本 支持SQLite(*.sqlite;*.db)数据库

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  5. 【C#】使用EF访问Sqlite数据库

    原文:[C#]使用EF访问Sqlite数据库 1. 先上Nuget下载对应的包 如图,搜索System.Data.SQLite下载安装即可,下载完之后带上依赖一共有这么几个: EntityFramew ...

  6. 如何在 Ubuntu 15.04 上安装带 JSON 支持的 SQLite 3.9

    欢迎阅读我们关于SQLite 的文章,SQLite 是当今世界上使用最广泛的 SQL 数据库引擎,它基本不需要配置,不需要设置或管理就可以运行.SQLite 是一个是公开领域(public-domai ...

  7. QT实现支持加密的Sqlite数据库引擎

    Sqlite数据库使用很广泛,我们经常会在发布一些小型软件的时候使用它,因为它不需要安装服务器.QT默认的数据库引擎是支持SQLITE数据库的,但并不支持对数据库加密,不加密的Sqlite数据库任何人 ...

  8. 【Win 10 应用开发】Sqlite 数据库的简单用法

    如果老周没记错的话,园子里曾经有朋友写过如何在 UWP 项目中使用 Sqlite数据库的文章.目前我们都是使用第三方封装的库,将来,SDK会加入对 Sqlite 的支持. 尽管目前 UWP-RT 库中 ...

  9. [开源].NET高性能框架Chloe.ORM-完美支持.NET Core

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

随机推荐

  1. Oracle数据库中实现mysql数据库中auto-increment功能

    在Mysql数据库中,想要实现一条数据的自增一功能(即插入此数据时填写null即可,系统自动+1),可直接在所在列使用语句auto-increment. id int primary key auto ...

  2. gcc杂谈

    1. -l选项自动给库文件名增加lib前缀和.a/.so后缀.所以如果你有一个lib叫做libusb.a,那么编译选项是-lusb.另一方面,如果你有一个文件叫做libusb.o(是目标文件而不是库文 ...

  3. bzoj1173: [Balkan2007]Point

    Description 给出N个三维空间上的点. 问有多少条直线,这些直线上至少有三个点. Input 第一行给出数字N,N在[4,1000] 下面N行,每行三个数字,用于描述点的坐标,其值在[-10 ...

  4. Python 派生类子类继承类

    1.创建list类的子类Namedlist,初始化新类,创建新对象实例johnny,检查对象类型,并使用list的一些功能来存储数据 >>> class Namedlist(list ...

  5. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  6. NotifyIcon用法

    -------------------控件NotifyIcon-----------//客户端调用 private void btnShowError_Click(object sender, Eve ...

  7. 浅析:setsockopt()改善socket网络程序的健壮性

    1. 如果在已经处于 ESTABLISHED状态下的socket(一般由端口号和标志符区分)调用closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BO ...

  8. foxmail 6.5升级到7.0版本后,旧邮件的导入处理

    随着foxmail 7.0版的火热升级,部分从foxmial 6.5版升级到7.0版的用户可能会出现旧邮件丢失的困扰.这里,foxmail为大家提供的解决方案如下:   打开Foxmail,点击 文件 ...

  9. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  10. SparkSql官方文档中文翻译(java版本)

    1 概述(Overview) 2 DataFrames 2.1 入口:SQLContext(Starting Point: SQLContext) 2.2 创建DataFrames(Creating ...