Entity Framework 6 with SQLite 3 Code First - Won't create tables

I started with the code from fried and created a project which lets you use CodeFirst. The project is available open source on GitHub or as NuGet Package.

If you encounter any problems or miss a feature, feel free to open a new issue on GitHub. Of course PRs are very welcome.

Edit (26.04.2016):

In the meantime I did a lot in this project.

The following (default EF) functionality is supported:

  • Tables from classes (supported annotations: Table)
  • Columns from properties (supported annotations: Column, Key, MaxLength, Required, NotMapped, DatabaseGenerated, Index)
  • PrimaryKey constraint (Key annotation, key composites are supported)
  • ForeignKey constraint (1-n relationships, support for 'Cascade on delete')
  • Not Null constraint
  • Auto increment (an int PrimaryKey will automatically be incremented)
  • Index (Decorate columns with the Index attribute. Indices are automatically created for foreign keys by default. To prevent this you can remove the convetion ForeignKeyIndexConvention)

There are also some features exclusive for SQLite, which are not supported by default:

  • Unique constraint (Decorate columns with the UniqueAttribute, which is part of this library)
  • Collate constraint (Decorate columns with the CollateAttribute, which is part of this library)

There are two ways to use the functionality of this library.

  1. Make use of the DbInitializers:

    • SqliteCreateDatabaseIfNotExists
    • SqliteDropCreateDatabaseAlways
    • SqliteDropCreateDatabaseWhenModelChanges
  2. Get more control by using one of the following two classes:

    • SqliteSqlGenerator (creates the SQL based on a EdmModel)
    • SqliteDatabaseCreator (creates a new SQLite database based on a Database and DbModel)

Edit (30.05.2020): As EF 6 now supports .NET Core 3 and above, I adjusted the library to support .NET Standard 2.1 as well. The following .NET framework versions are supported:

  • .NET 4.0 (uses net40)
  • .NET 4.5-4.8 (uses net45)
  • .NET Core 3.0-3.1 (uses netstandard2.1)

Unfortunately, the EF6 provider implementation in System.Data.SQLite.EF6 doesn't support creating tables. I downloaded the SQLite source code to have a look but couldn't find anything for creating tables and for migrations. The EF6 provider is basically the same as their Linq implementation so it's all aimed at querying the database rather than modifying it.

I currently do all of my work with SQL Server and generate sql scripts for SQLite using the SQL Server Compact & SQLite Toolbox. The scripts can then be run using an SQLiteCommand to simulate migrations.

Update

In EF7 support for SQL server compact has been dropped and a new provider for SQLite is being developed by the EF team. The provider will use Microsoft's managed SQLite wrapper project, Microsoft.Data.SQLite rather than the System.Data.SQLite project. This will also allow for using EF7 on iOS, Android, Windows Phone / Mobile, Linux, Mac etc. as Microsoft's wrapper is being developed as a portable library.

It's all still in beta but you can get nuget packages from the ASP.Net development feeds at MyGet (dev, master, release) if you wish to have a look. Look for the EntityFramework.SQLite package.


I decided to write my own rudimentary database initializer to solve this problem.

You can check it out here: https://gist.github.com/flaub/1968486e1b3f2b9fddaf

It supports:

  • Many-to-Many relationships
  • Code-First data annotations like:
    • [Key]
    • [Required]
    • [Index]
    • [ForeignKey]