All Projects → msallin → Sqlitecodefirst

msallin / Sqlitecodefirst

Licence: apache-2.0
Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Sqlitecodefirst

EFCore.Sqlite.NodaTime
Adds support for NodaTime types when using SQLite with Entity Framework Core.
Stars: ✭ 21 (-96.01%)
Mutual labels:  sqlite, entity-framework
Sqlcetoolbox
SQLite & SQL Server Compact Toolbox extension for Visual Studio, SSMS (and stand alone)
Stars: ✭ 651 (+23.76%)
Mutual labels:  entity-framework, sqlite
sqlite-dotnet-core
.NET Core 2.1 Console Application using SQLite with Entity Framework and Dependency Injection
Stars: ✭ 17 (-96.77%)
Mutual labels:  sqlite, entity-framework
Entityworker.core
EntityWorker is an object-relation mapper(ORM) that enable .NET developers to work with relations data using objects. EntityWorker is an alternative to entityframwork. is more flexible and much faster than entity framework.
Stars: ✭ 91 (-82.7%)
Mutual labels:  entity-framework, sqlite
Entityframework.exceptions
Handle database errors easily when working with Entity Framework Core. Supports SQLServer, PostgreSQL, SQLite, Oracle and MySql
Stars: ✭ 266 (-49.43%)
Mutual labels:  entity-framework, sqlite
Android Orma
An ORM for Android with type-safety and painless smart migrations
Stars: ✭ 442 (-15.97%)
Mutual labels:  sqlite
Maghead
The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7
Stars: ✭ 483 (-8.17%)
Mutual labels:  sqlite
Bolt
Bolt is a simple CMS written in PHP. It is based on Silex and Symfony components, uses Twig and either SQLite, MySQL or PostgreSQL.
Stars: ✭ 4,136 (+686.31%)
Mutual labels:  sqlite
Pytablewriter
pytablewriter is a Python library to write a table in various formats: CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
Stars: ✭ 422 (-19.77%)
Mutual labels:  sqlite
Nimforum
Lightweight alternative to Discourse written in Nim
Stars: ✭ 523 (-0.57%)
Mutual labels:  sqlite
Postfixadmin
PostfixAdmin - web based virtual user administration interface for Postfix mail servers
Stars: ✭ 509 (-3.23%)
Mutual labels:  sqlite
Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (-9.32%)
Mutual labels:  sqlite
Androidwithkotlin
🚀 These are android sample projects which are written in Kotlin. It covers video streaming, mp3 player, sqlite, location services, custom camera, o-notifications, simple compass etc.
Stars: ✭ 447 (-15.02%)
Mutual labels:  sqlite
Rbatis
Rust ORM Framework High Performance Rust SQL-ORM(JSON based)
Stars: ✭ 482 (-8.37%)
Mutual labels:  sqlite
Selda
A type-safe, high-level SQL library for Haskell
Stars: ✭ 435 (-17.3%)
Mutual labels:  sqlite
Adminer
Database management in a single PHP file
Stars: ✭ 4,999 (+850.38%)
Mutual labels:  sqlite
Sqlprovider
A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
Stars: ✭ 423 (-19.58%)
Mutual labels:  sqlite
Django Dbbackup
Management commands to help backup and restore your project database and media files
Stars: ✭ 471 (-10.46%)
Mutual labels:  sqlite
Denodb
MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Stars: ✭ 498 (-5.32%)
Mutual labels:  sqlite
Osharp V4
osharp framework with .net framework version 4.x
Stars: ✭ 472 (-10.27%)
Mutual labels:  entity-framework

SQLite CodeFirst

Release Build Build status

CI Build Build status

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

Support the project PayPal donate button

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 and .NET Standard 2.1. You can use the SQLite CodeFirst in projects that target the following frameworks:

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

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

.NET Core example

Add the following package references.

<PackageReference Include="System.Data.SQLite" Version="1.0.112.2" />
<PackageReference Include="System.Data.SQLite.EF6" Version="1.0.112.2" />

Add the following class.

public Configuration()
{
    SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
    SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);

    var providerServices = (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices));

    SetProviderServices("System.Data.SQLite", providerServices);
    SetProviderServices("System.Data.SQLite.EF6", providerServices);

    SetDefaultConnectionFactory(this);
}

public DbConnection CreateConnection(string connectionString)
    => new SQLiteConnection(connectionString);
}

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].