Simple example using System.Data.SQLite with Entity Framework 6

前端 未结 2 1971
走了就别回头了
走了就别回头了 2020-12-23 21:13

I am trying to get a simple code first example to work in a console app using SQLite and EF6, however I am running into multiple errors: I created a new console project in V

2条回答
  •  伪装坚强ぢ
    2020-12-23 21:39

    A similar question is asked over here: Entity Framework 6 with SQLite 3 Code First - Won't create tables

    kjbartel gives very useful explanation that table creation is not supported by the EF SQLite Driver.

    Also see https://github.com/msallin/SQLiteCodeFirst, which provides an excellent solution. I installed the SQLite.CodeFirst NuGet package, and added the below code, then the app works fine:

        class MyContext : DbContext
        {
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists(modelBuilder);
                Database.SetInitializer(sqliteConnectionInitializer);
            }
            public DbSet Persons { get; set; }
        }
    

提交回复
热议问题