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

痴心易碎 提交于 2019-11-26 18:39:15

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 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)

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!