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

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


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<MyContext>(modelBuilder);            Database.SetInitializer(sqliteConnectionInitializer);        }        public DbSet<Person> Persons { get; set; }    }


You need to initialize your database with the tables for your models. Notice the error "SQL logic error or missing database no such table: People".

That means you need to run SQL to create the corresponding tables in the database, thankfully, if using VS, under the context menu while in the model editor (*.edmx files), there is an option to have it auto-generate the SQL and execute it to create the table entries in the database for you based on the model. Note: sometimes the auto-generated for non MS-SQL can have issues that need to be manually fixed before it will compile/run.