Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/msallin/sqlitecodefirst
Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.
https://github.com/msallin/sqlitecodefirst
codefirst csharp entity-framework sqlite sqlite-codefirst
Last synced: 2 days ago
JSON representation
Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.
- Host: GitHub
- URL: https://github.com/msallin/sqlitecodefirst
- Owner: msallin
- License: apache-2.0
- Created: 2015-03-22T19:30:21.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-02-23T12:39:34.000Z (11 months ago)
- Last Synced: 2024-10-30T01:37:46.974Z (3 months ago)
- Topics: codefirst, csharp, entity-framework, sqlite, sqlite-codefirst
- Language: C#
- Size: 1020 KB
- Stars: 610
- Watchers: 40
- Forks: 123
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# SQLite CodeFirst
**Release Build** [![Build status](https://ci.appveyor.com/api/projects/status/2qavdqctw0ehscm6/branch/master?svg=true)](https://ci.appveyor.com/project/msallin/sqlitecodefirst-nv6vn/branch/master)
**CI Build** [![Build status](https://ci.appveyor.com/api/projects/status/oc1miog385h801qe?svg=true)](https://ci.appveyor.com/project/msallin/sqlitecodefirst)
Creates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst.
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.## Project Status
This project was started when there was .NET Full Framework and EF 6. EF 6 does not offer code first for SQLite and this library fills this gab.
Nowadays there is .NET Core (or now just called .NET) and EF Core. EF Core supports code first and migrations for SQLite.
If you use .NET Core 3 or above together with EF Core, there is no need for this library.
If you use EF 6 (either with .NET Full Framework or with .NET Core 3 or above), this library is an option for you to get code first for SQLite.
I'm going to maintain this library as long as it is useful for some people (see [History](https://github.com/msallin/SQLiteCodeFirst/issues/166) & [Project Status and Release Schedule](https://github.com/msallin/SQLiteCodeFirst/issues/157)).## 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.)
- Default collation (pass an instance of Collation as constructor parameter for an initializer to specify a default collation).
- 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](https://github.com/msallin/SQLiteCodeFirst/releases) or install the NuGet-Package [SQLite.CodeFirst](https://www.nuget.org/packages/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)
- .NET 5-8 (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
- SqliteDropCreateDatabaseWhenModelChangesIf 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
```csharp
public class MyDbContext : DbContext
{
public MyDbContext()
: base("ConnectionStringName") { }protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists(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.```csharp
public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways
{
public MyDbContextInitializer(DbModelBuilder modelBuilder)
: base(modelBuilder) { }protected override void Seed(MyDbContext context)
{
context.Set().Add(new Player());
}
}
```### SqliteDatabaseCreator Sample
```csharp
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
```csharp
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.
```xml```
Add the following class.
```csharp
public class MyConfiguration : DbConfiguration, IDbConnectionFactory {
public MyConfiguration()
{
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);
}
}
```Also, make sure you specify the DbConfigurationType on the DBContext class as well
```csharp
[DbConfigurationType(typeof(MyConfiguration))]
public class Context: DbContext {
//... DBContext things
}
```
## StructureThe 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](https://www.sqlite.org/lang.html).
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:## Recognition
I started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub).