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


    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>();
                }
            }
        }
  • 相关阅读:
    文件上传案例_Socket_测试
    Linux的小整理(入门)
    full stack 第一天
    基础考题 试题
    shell语法
    网络管理
    图像类
    定时储存
    网络管理
    磁盘管理
  • 原文地址:https://www.cnblogs.com/zt199510/p/12773868.html
Copyright © 2020-2023  润新知