|
1 | 1 | # SQLiteEfCodeFirstDbCreator |
2 | | -Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst. |
3 | | - |
| 2 | +Creates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst. |
4 | 3 |
|
5 | 4 | This Project ships a "SqliteContextInitializer" which creates a new SQLite Database, based on your model/code. |
6 | 5 | I started with the code from flaub: https://gist.github.com/flaub/1968486e1b3f2b9fddaf |
7 | 6 |
|
8 | 7 | Currently the following is supported: |
9 | 8 | - Tables from classes |
10 | 9 | - Columns from properties |
11 | | -- 1-n relationships |
| 10 | +- ForeignKey constraint (1-n relationships) |
12 | 11 | - Cascade on delete |
13 | | -- PrimaryKey constraint |
| 12 | +- PrimaryKey constraint (An int PrimaryKey will automatically be incremented) |
14 | 13 | - Not Null constraint |
15 | 14 |
|
16 | 15 | I tried to write the code in a extensible way. |
17 | 16 | The logic is devided into two main parts. Builder and Statement. |
18 | | -The Builder-Part knows how to translate the EdmModel into statements where a statement class creates the SQLite-SQL-Code. |
| 17 | +The Builder-Part knows how to translate the EdmModel into statements where a statement class creates the SQLite-SQL-Code. The structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html). |
| 18 | + |
| 19 | +## How to use |
| 20 | +If you want to let the Entity Framework create the database, if it does not exist, just set `SqliteContextInitializer<>` as your `IDbInitializer`. |
| 21 | +```csharp |
| 22 | +public class MyDbContext : DbContext |
| 23 | +{ |
| 24 | + public TestDbContext() |
| 25 | + : base("ConnectionStringName") { } |
| 26 | + |
| 27 | + protected override void OnModelCreating(DbModelBuilder modelBuilder) |
| 28 | + { |
| 29 | + var sqliteConnectionInitializer = new SqliteContextInitializer<TestDbContext>( |
| 30 | + Database.Connection.ConnectionString, modelBuilder); |
| 31 | + Database.SetInitializer(sqliteConnectionInitializer); |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +In a more advanced szenario you may want to populate some core- or test-data after the database was created. |
| 37 | +To do this, inherit from `SqliteContextInitializer<>` |
| 38 | +```csharp |
| 39 | +public class TestDbContextInitializer : SqliteContextInitializer<TestDbContext> |
| 40 | +{ |
| 41 | + public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder) |
| 42 | + : base(connectionString, modelBuilder) { } |
| 43 | + |
| 44 | + protected override void Seed(TestDbContext context) |
| 45 | + { |
| 46 | + context.Set<Player>().Add(new Player()); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
0 commit comments