• 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>();
                }
            }
        }
  • 相关阅读:
    OnFileOpen与OnOpenDocument(转)
    Wpf应用程序进入全屏和退出全屏
    在WPF中使用Emgu加载Image<,>图像的两种方法
    C#中ListBox控件重绘Item项
    sdut2404 Super Prime ACM算法设计
    真彩色制式下IplImage转成CBitmap格式
    【转】中缀表达式转换为后缀表达式
    MFC中CImage的简单复制方法 (Copy CImage)
    齐鲁软件大赛尖峰时刻团队
    Priest John's Busiest Day HDU2491 ACM算法设计
  • 原文地址:https://www.cnblogs.com/zt199510/p/12773868.html
Copyright © 2020-2023  润新知