SQLiteCodeFirst

Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.

Github星跟蹤圖

SQLite CodeFirst

Release Build Build status

CI Build Build status

Creates a SQLite Database from Code, using Entity Framework CodeFirst.

Support the project

To support this project you can: star the repository, report bugs/request features by creating new issues, write code and create PRs or donate.
Especially if you use it for a commercial project, a donation is welcome.
If you need a specific feature for a commercial project, I am glad to offer a paid implementation.

Features

This project ships several IDbInitializer classes. These create new SQLite Databases based on your model/code.

The following features are 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 and you can explicit set the "AUTOINCREMENT" constraint to a PrimaryKey using the Autoincrement-Attribute)
  • Index (Decorate columns with the Index attribute. Indices are automatically created for foreign keys by default. To prevent this you can remove the convention ForeignKeyIndexConvention)
  • 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. Use CollationFunction.Custom to specify a own collation function.)
  • SQL default value (Decorate columns with the SqlDefaultValueAttribute, which is part of this library)

Install

Either get the assembly from the latest GitHub Release Page or install the NuGet-Package SQLite.CodeFirst (PM> Install-Package SQLite.CodeFirst).

The project is built to target .NET framework versions 4.0 and 4.5.
You can use the SQLite CodeFirst in projects that target the following frameworks:

  • .NET 4.0 (uses net40)
  • .NET 4.5 (uses net45)
  • .NET 4.5.1 (uses net45)
  • .NET 4.5.2 (uses net45)
  • .NET 4.6 (uses net45)
  • .NET 4.6.1 (uses net45)
  • .NET 4.6.2 (uses net45)
  • .NET 4.7 (uses net45)
  • .NET 4.7.1 (uses net45)
  • .NET 4.7.2 (uses net45)
  • .NET 4.8 (uses net45)

How to use

The functionality is exposed by using implementations of the IDbInitializer<> interface.
Depending on your need, you can choose from the following initializers:

  • SqliteCreateDatabaseIfNotExists
  • SqliteDropCreateDatabaseAlways
  • SqliteDropCreateDatabaseWhenModelChanges

If you want to have more control, you can use the SqliteDatabaseCreator (implements IDatabaseCreator) which lets you control the creation of the SQLite database.
Or for even more control, use the SqliteSqlGenerator (implements ISqlGenerator), which lets you generate the SQL code based on your EdmModel.

When you want to let the Entity Framework create database if it does not exist, just set SqliteDropCreateDatabaseAlways<> or SqliteCreateDatabaseIfNotExists<> as your IDbInitializer<>.

Initializer Sample

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

Notice that the SqliteDropCreateDatabaseWhenModelChanges<> initializer will create a additional table in your database.
This table is used to store some information to detect model changes. If you want to use an own entity/table you have to implement the
IHistory interface and pass the type of your entity as parameter to the constructor of the initializer.

In a more advanced scenario, you may want to populate some core- or test-data after the database was created.
To do this, inherit from SqliteDropCreateDatabaseAlways<>, SqliteCreateDatabaseIfNotExists<> or SqliteDropCreateDatabaseWhenModelChanges<> and override the Seed(MyDbContext context) function.
This function will be called in a transaction, once the database was created. This function is only executed if a new database was successfully created.

public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
{
    public MyDbContextInitializer(DbModelBuilder modelBuilder)
        : base(modelBuilder) { }

    protected override void Seed(MyDbContext context)
    {
        context.Set<Player>().Add(new Player());
    }
}

SqliteDatabaseCreator Sample

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var model = modelBuilder.Build(Database.Connection);
        IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();
        sqliteDatabaseCreator.Create(Database, model);
    }
}

SqliteSqlGenerator Sample

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var model = modelBuilder.Build(Database.Connection);
        ISqlGenerator sqlGenerator = new SqliteSqlGenerator();
        string sql = sqlGenerator.Generate(model.StoreModel);
    }
}

Structure

The code is written in an extensible way.
The logic is divided into two main parts, Builder and Statement.
The Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code.
The structure of the statements is influenced by the SQLite Language Specification.
You will find an extensive usage of the composite pattern.

Hints

If you try to reinstall the NuGet-Packages (e.g. if you want to downgrade to .NET 4.0), the app.config will be overwritten and you may getting an exception when you try to run the console project.
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.

Recognition

I started with the code from flaub.

主要指標

概覽
名稱與所有者msallin/SQLiteCodeFirst
主編程語言C#
編程語言C# (語言數: 1)
平台
許可證Apache License 2.0
所有者活动
創建於2015-03-22 19:30:21
推送於2024-02-23 12:39:34
最后一次提交2024-02-23 13:08:55
發布數28
最新版本名稱v1.7.0.36 (發布於 )
第一版名稱v0.1-apha.3 (發布於 )
用户参与
星數617
關注者數40
派生數122
提交數291
已啟用問題?
問題數114
打開的問題數4
拉請求數53
打開的拉請求數0
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?