如何用Entity Framework 6 连接Sqlite数据库
来源:互联网 发布:软件行业税负 编辑:程序博客网 时间:2024/06/03 19:04
获取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
- 如何用Entity Framework 6 连接Sqlite数据库
- 让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
- 让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
- Entity Framework 6 连接Sqlite数据库遇到的问题
- Entity Framework 连接操作 Sqlite 数据库
- Entity framework5 连接sqlite数据库
- 如何用spring连接数据库
- Entity framework sqlite
- Entity Framework 和 Sqlite
- 如何用vb语句连接SQL数据库
- 如何用java连接sql的数据库
- 如何用c++连接sql2000数据库
- 如何用JDBC连接数据库操作
- 如何用sqlplus远程连接数据库(转载)
- 如何用Eclipse连接MySQL数据库
- 如何用Eclipse连接MySQL数据库
- 如何用JDBC连接到数据库
- 如何用R连接mysql数据库
- Swift自适应布局(Adaptive Layout)教程(一)Autolayout
- Swift自适应布局(Adaptive Layout)教程(二)Autolayout
- CSS 之 核心篇
- Eclipse 常用快捷键 (动画讲解)
- java程序员在面试时的注意事项
- 如何用Entity Framework 6 连接Sqlite数据库
- Spring 注解 @Resource和@Autowired的区别
- C# DataTable详细用法
- QTableWidget用法总结
- 产品经理入门到大神的资料全推荐
- #include<bits/stdc++.h>包含C++的所有头文件
- 大话数据结构学习之(二) 线性表
- qt对所有文字形式的文本使用tr()
- BZOJ 1015 JSOI2008 星球大战 starwar 并查集