1首先在app.config配置写入add
1:<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
2:<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" />
2:需要引动的DLL
3:附上代码:
class Program { static void Main(string[] args) { DbConnection dbConnection_1 = new SQLiteConnection("data source=D:\SqLiteCoFirst1.db"); var ctx1 = new mybookdb(dbConnection_1); ctx1.Books.Add(new book() { Author="123" }); Console.WriteLine(ctx1.SaveChanges()); Console.WriteLine(JsonConvert.SerializeObject(ctx1.Books.ToList())); Console.ReadLine(); } } class book { public int id { get; set; } public string Name { get; set; }//书名 public DateTime PublishDate { get; set; }//出版日期 public string Author { get; set; }//作者 public float Price { get; set; }//价格 } class mybookdb : DbContext { public DbSet<book> Books { get; set; } static string dbPath = $"Data Source=E:\thumbnail.db"; public mybookdb(DbConnection dbConnection,bool contextOwnsConnection=true):base(dbConnection,contextOwnsConnection) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Database.SetInitializer(new SqliteCreateDatabaseIfNotExists<ThumbnailContext>(modelBuilder)); // ModelConfiguration.Configure(modelBuilder); var init = new SqliteCreateDatabaseIfNotExists<mybookdb>(modelBuilder); Database.SetInitializer(init); } public class ModelConfiguration { public static void Configure(DbModelBuilder modelBuilder) { ConfigureBookEntity(modelBuilder); } private static void ConfigureBookEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<book>(); } } }