1、安装NuGet包
- System.Data.SQLite(System.Data.SQLite.Core、System.Data.SQLite.EF6、System.Data.SQLite.Linq )
- SQLite.CodeFirst
- EntityFramework
2、配置App.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> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> </startup> <entityFramework> <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" /> <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 (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" /> <remove invariant="System.Data.SQLite" /> <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> </DbProviderFactories> </system.data> <connectionStrings> <add name="EFDbContext" connectionString="Data Source=Database.db" providerName="System.Data.SQLite.EF6"/> </connectionStrings> </configuration>
在app.config中手动添加高亮部分代码,DataSource修改成对应的数据库文件名
3、创建实体类
[Table("DeptInfo")] // 标识数据库创建的表名 public class DeptInfo { public string SubDept { get; set; } public string DeptName { get; set; } public string DeptCode { get; set; } public string RuleCode { get; set; } [Key] // 主键 [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // 自增列 public int DeptId { get; set; } }
4、创建EFDbContext对象
public class EFDbContext : DbContext { public EFDbContext():base("EFDbContext"){} //配置使用的连接名 protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 如果数据表存在多列组合主键时,需要进行描述 // modelBuilder.Entity<DeptInfo>().HasKey(t => new { t.DeptId, t.DeptCode}); // CodeFirst创建数据表 var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<ORMContext>(modelBuilder); Database.SetInitializer(sqliteConnectionInitializer); } public DbSet<DeptInfo> DeptInfos{ set; get; } }
执行数据查询前,如果数据库文件不存在或者表不存在,不要执行context.Database.CreateIfNotExists(),否则创建表会失败,程序会抛出SQL logic error no such table: DeptInfo,后面即使屏蔽该行代码一样会抛异常,除非手动创建表或者删除数据库文件后重新生成。
5、批量插入数据
using(var ctx = new EFDbContext()) { ctx.Configuration.AutoDetectChangesEnabled = false; // 局部关闭DetectChanges方法,使EF不会跟踪实体,这样将不会造成全盘扫描而使得我们不会处于漫长的等待 ctx.DeptInfos.AddRange(deptInfos); // 批量添加 ctx.SaveChanges(); }
标签:SQLite,set,get,C#,EFDbContext,数据表,public From: https://www.cnblogs.com/zhengzc/p/17822335.html