最近在用winform,有些数据须要本地存储,因此想到了使用sqlite这个文件数据库。在使用Nuget安装sqlite的时候,发现会将Ef也安装上了,因此想着使用EF进行数据的操做吧,因此这就来了,各类坑。html
首先使用Nuget安装sqlite。安装成功后如图所示:mysql
安装后,你会发如今app.config中,添加关于sqlite的配置。sql
添加测试类以及数据上下文。数据库
public class Person { [Key] public Guid Id { set; get; } public string Name { set; get; } }
public class RetailContext : DbContext { public RetailContext() : base("SqliteTest") { } public DbSet<Person> Persons { set; get; } }
最终的app.configapp
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,
Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="SqliteTest" connectionString="Data Source=E:\retail.db" providerName="System.Data.SQLite.EF6" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v13.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" /> <!-- 1. Solves SQLite error of "Unable to find the requested .Net Framework Data Provider."--> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </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>
关于其中修改的部分能够参考这篇文章ide
http://www.cnblogs.com/lifeil/p/3944334.htmlsqlserver
好了,如今咱们插入一条数据测试
using (Retail.Data.RetailContext context = new Data.RetailContext()) { context.Persons.Add(new Person { Id = Guid.NewGuid(), Name = "wolfy1" }); context.SaveChanges(); }
出错了ui
错误信息spa
{"SQL logic error or missing database\r\nno such table: People"}
哪来的People表?
而后就是各类搜索,发现sqlite在建立表的时候,默认是使用实体类的复数形式进行建立,person的复数不是persons么(对开发人员来讲),而sqlite不按套路出牌,人家认为Person的复数就是People,至关无语了,欺负我英语很差么?
那咱们若是使用特性指定数据表的名字行不行,修改实体类
[Table("Persons")] public class Person { [Key] public Guid Id { set; get; } public string Name { set; get; } }
结果成功了,看来在使用sqlite和ef6的时候,须要指定table特性,并设置对应的数据表名称。
那咱们看另一个,咱们添加一个Student类
public class Student { [Key] public Guid Id { set; get; } public string Name { set; get; } }
注意,这里并无设定数据表,没有为其添加Table特性。
using (Retail.Data.RetailContext context = new Data.RetailContext()) { context.Students.Add(new Student { Id = Guid.NewGuid(), Name = "wolfy1" }); context.SaveChanges(); }
结果
这又成功了。让人搞不懂,在使用ef+mysql,或者sqlserver的时候,默认是使用数据上下文中指定的属性名称做为数据表名称的。但sqlite不行,若是不指定Table特性设置数据表名称,就欺负你英语很差的了,怎么地了?
public class RetailContext : DbContext { public RetailContext() : base("SqliteTest") { } public DbSet<Student> Students { set; get; } public DbSet<Person> Persons { set; get; } }
在EF6+SQLITE3的时候,注意指定对应实体类对应的Table特性显示指定表名称。
资料