让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
获取Sqlite
1.可以用NuGet程序包来获取,它也会自动下载EF6

2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
注意这里面每个.net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装的DLL里面包含SQLite.Interop.dll,而另一个没有。如果你运行代码的时候报
“无法加载SQLite.Interop.dll”的错误,则将安装文件中的SQLite.Interop.dll拷贝到Bin文件中即可。或是在NuGet下载的packages\System.Data.SQLite.Core.1.0.94.0\build中也有对应的程序。
示例代码
Model.cs
public class Person
{
public Int64 Id { get; set; } //注意要用Int64
public string FirstName { get; set; }
public string LastName { get; set; }
} public class MyContext : DbContext
{
public DbSet<Person> Persons { get; set; } public MyContext()
: base("SqliteTest")
{ }
}
Program.cs
static void Main(string[] args)
{
MyContext context = new MyContext();
var empList = context.Persons.OrderBy(c => c.FirstName).ToList();
Console.WriteLine(empList.Count); Person people = new Person()
{
FirstName = "Hello",
LastName = "World"
};
context.Persons.Add(people);
context.SaveChanges();
Console.ReadLine();
}
示例代码很简单,就是用EF对Person表进行新增与查看。
配置config文件
如果你是用NuGet获取Sqlite,会自动在config中配置一些相关的信息。
<?xml version="1.0" encoding="utf-8"?>
<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>
<connectionStrings>
<add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<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" />
<remove invariant="System.Data.SQLite" />
<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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
</configuration>
其中数据连接串是:
<add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
注意提供程序集是System.Data.SQLite.EF6。
但是这个配仍然是错误的。
如果此时运行程序,会报错:
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.
或中文错误信息:
未找到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序。
意思是EF没有找到提供System.Data.SQLite.SQLiteFactory的dll,我们看看现在config中的entityFramework节点:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
有System.Data.SQLite.EF6与System.Data.SqlClient,确实没有名称为System.Data.SQLite的提供程序。这里我一直不明白为什么sqlite会去找名称为System.Data.SQLite的提供程序,因为我们在连接串中配置的provider也是System.Data.SQLite.EF6。
那我们就在EF的配置节点中增加一个名为System.Data.SQLite的provider,但type仍然是System.Data.SQLite.EF6。最终的配置如图:

红色部分是配置有变化的地方。
这里再运行程序就可以了。
注意:
1.连接串的配置。
数据连接串可以指定绝对地址,也可以指定相对地址。像我的data source=SqliteTest.db,则SqliteTest.db要在Bin文件夹中,如果是web程序可以通过Data Source=|DataDirectory|\SqliteTest.db来配置在App_Data文件平中。
2.如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。
3.不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。
示例代码(packages文件太大,所以删除了):SqliteLinqTest.zip
让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库的更多相关文章
- 如何用Entity Framework 6 连接Sqlite数据库[转]
获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...
- UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库
在应用中使用 SQLite 数据库来存储数据是相当常见的.在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLit ...
- 在服务器中使用 Entity Framework 的 Migration 更新数据库
在开发环境中,每次我们对要对数据库进行更改,比如增加修改表字段等.改好Entity类后,我们只需在Nuget程序包管理控制台运行 update-database 脚本却可: update-databa ...
- 使用 Entity Framework 7 进行 SQLite 的 CURD 操作
原文地址:http://www.oschina.net/translate/sqlite-crud-operation-using-entity-framework 介绍 我善于使用传统的SQL查询风 ...
- Asp.net core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库
Asp.net Core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库 1.前言 本次主要采用Asp.net core3.1 ...
- Entity Freamwork 6连接PostgreSql数据库
原文 Entity Freamwork 6连接PostgreSql数据库 开发环境 VS 2015 Update 1 Postgre Sql 9.4 使用过程 1.使用Nuget在项目中添加对E ...
- 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)
前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...
- EF框架组件详述【Entity Framework Architecture】(EF基础系列篇3)
我们来看看EF的框架设计吧: The following figure shows the overall architecture of the Entity Framework. Let us n ...
- 1.翻译:EF基础系列--什么是Entity Framework?
大家好,好久不见,EF系列之前落下了,还是打算重新整理一下. 先说说目前的打算:先简单了解一下EF基础系列-->然后就是EF 6 Code-First系列-->接着就是EF 6 DB-Fi ...
随机推荐
- centos
CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linu ...
- 【转】js实现复制到剪贴板功能,兼容所有浏览器
两天前听了一个H5的分享,会议上有一句话,非常有感触:不是你不能,而是你对自己的要求太低.很简单的一句话,相信很多事情不是大家做不到,真的是对自己的要求太低,如果对自己要求多一点,那么你取得的进步可能 ...
- C++ 一个例子彻底搞清楚拷贝构造函数和赋值运算符重载的区别
class TestChild { public: TestChild() { x=; y=; printf("TestChild: Constructor be called!\n&quo ...
- jeecg小吐槽续——自己折腾修改在线开发功能中“默认值”的使用
-- 原来设置了"默认值"的字段,新建表单时不会出现在表单上,要保存后才能在列表页面出现,而且第二次编辑时,设置了"默认值"的字段再也不能改成空值! -- 要修 ...
- 解决:IOError: [Errno 28] No space left on device(设备空间不足)
问题重现: 问题分析: 出现这样的问题,是磁盘空间不足,需要清理.卸载一下系统非必要软件和文件. 解决方案: 可以使用如下命令清理: #自动清理残余的依赖库 sudo apt-get autoremo ...
- yii 核心类classes.php详解(持续更新中...)
classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...
- CSS3-transform 转换/变换
transform 向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 兼容性: Internet Explorer 10.Firefox.Opera 支持 trans ...
- 仿QQ大战—服务器的搭建(ServerSocket)
ServerSocket(服务器): ServerSocket是JAVA中提供的用于建立服务器的类: 在客户/服务器通信模式中, 服务器端需要创建监听端口的 ServerSocket, ServerS ...
- python 2.6 与 2.4 区别
class 要把 class config(): 改成 class config: except Exception as e # only works in python 2.4 to 2.7 tr ...
- 收集的在线图片压缩(jpg/png)
http://www.yasuotu.com/ http://www.jpegmini.com/ http://www.tumiaoya.com/ https://tinypng.com/(推荐) h ...