HOWTO: SQLite with EntityFramework and Code-First HOWTO: SQLite with EntityFramework and Code-First sqlite sqlite

HOWTO: SQLite with EntityFramework and Code-First


System.Data.SQLite is in current version only EFv1 compatible provider (1, 2). It means it doesn't have EFv4 (EFv4.2 is wrapper around EFv4) features including database creation and deletion (check DbProviderServices differences for .NET 3.5 and .NET 4.0 in MSDN). Because of that you cannot use automatic database creation with code first when working with SQLite. You must create database and all tables manually.

As an alternative you can use different provider for SQLite. For example Devart's SQLite provider claims it supports EFv4 and 4.1 and thus database creation.


Check this out:

https://github.com/msallin/SQLiteCodeFirst

Also available as a NuGet

It adds some DB Initializers to your ptoject, such as SqliteCreateDatabaseIfNotExists

Here's the example of the web site:

public class MyDbContext : DbContext{    public MyDbContext()        : base("ConnectionStringName") { }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {        var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>(            Database.Connection.ConnectionString, modelBuilder);        Database.SetInitializer(sqliteConnectionInitializer);    }}