net Entityframerwork+sqlite 数据库迁移配置(采坑日记)

1首先在app.config配置写入add

net Entityframerwork+sqlite 数据库迁移配置(采坑日记)_第1张图片

 

 

1:
2:

  2:需要引动的DLL

net Entityframerwork+sqlite 数据库迁移配置(采坑日记)_第2张图片

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 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(modelBuilder));
           // ModelConfiguration.Configure(modelBuilder);
            var init = new SqliteCreateDatabaseIfNotExists(modelBuilder);
            Database.SetInitializer(init);
        }

        public class ModelConfiguration
        {
            public static void Configure(DbModelBuilder modelBuilder)
            {
                ConfigureBookEntity(modelBuilder);
            }
            private static void ConfigureBookEntity(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity();
            }
        }
    }

 

你可能感兴趣的:(net Entityframerwork+sqlite 数据库迁移配置(采坑日记))