|
1 | 1 | # SQLite CodeFirst |
| 2 | +**Release Build** [](https://ci.appveyor.com/project/msallin/sqlitecodefirst-nv6vn/branch/master) |
| 3 | + |
| 4 | +**CI Build** [](https://ci.appveyor.com/project/msallin/sqlitecodefirst) |
| 5 | + |
2 | 6 | Creates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst. |
3 | 7 |
|
4 | | -This Project ships a `SqliteContextInitializer` which creates a new SQLite Database, based on your model/code. |
| 8 | +This Project ships several `IDbInitializer` which creates a new SQLite Database, based on your model/code. |
5 | 9 | I started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub). |
6 | 10 |
|
7 | 11 | Currently the following is supported: |
8 | | -- Tables from classes (with table annotation) |
9 | | -- Columns from properties |
10 | | -- ForeignKey constraint (1-n relationships) |
11 | | -- Cascade on delete |
12 | | -- PrimaryKey constraint (An int PrimaryKey will automatically be incremented) |
| 12 | +- Tables from classes (supported annotations: `Table`) |
| 13 | +- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`, `NotMapped`, `DatabaseGenerated`) |
| 14 | +- PrimaryKey constraint (`Key` annotation, key composites are supported) |
| 15 | +- ForeignKey constraint (1-n relationships, support for 'Cascade on delete') |
13 | 16 | - Not Null constraint |
| 17 | +- Auto increment (An int PrimaryKey will automatically be incremented) |
14 | 18 |
|
15 | 19 | I tried to write the code in a extensible way. |
16 | | -The logic is devided into two main parts. Builder and Statement. |
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). |
| 20 | +The logic is divided into two main parts. Builder and Statement. |
| 21 | +The Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code. |
| 22 | +The structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html). |
| 23 | +You will find an extensive usage of the composite pattern. |
18 | 24 |
|
19 | 25 | ## 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`. |
| 26 | +Either get the assembly from the latest [release](https://github.com/msallin/SQLiteCodeFirst/releases) or install the NuGet-Package [SQLite.CodeFirst](https://www.nuget.org/packages/SQLite.CodeFirst/). |
| 27 | + |
| 28 | +If you want to let the Entity Framework create the database, if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`. |
21 | 29 | ```csharp |
22 | 30 | public class MyDbContext : DbContext |
23 | 31 | { |
24 | | - public TestDbContext() |
| 32 | + public MyDbContext() |
25 | 33 | : base("ConnectionStringName") { } |
26 | 34 |
|
27 | 35 | protected override void OnModelCreating(DbModelBuilder modelBuilder) |
28 | 36 | { |
29 | | - var sqliteConnectionInitializer = new SqliteContextInitializer<TestDbContext>( |
| 37 | + var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>( |
30 | 38 | Database.Connection.ConnectionString, modelBuilder); |
31 | 39 | Database.SetInitializer(sqliteConnectionInitializer); |
32 | 40 | } |
33 | 41 | } |
34 | 42 | ``` |
35 | 43 |
|
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<>` and override the `Seed(TestDbContext context)` function. |
| 44 | +In a more advanced scenario you may want to populate some core- or test-data after the database was created. |
| 45 | +To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function. |
38 | 46 | This function will be called, in a own transaction, right after the database was created. This function is only executed if a new database was successfully created. |
39 | 47 | ```csharp |
40 | | -public class TestDbContextInitializer : SqliteContextInitializer<TestDbContext> |
| 48 | +public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext> |
41 | 49 | { |
42 | | - public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder) |
| 50 | + public MyDbContextInitializer(string connectionString, DbModelBuilder modelBuilder) |
43 | 51 | : base(connectionString, modelBuilder) { } |
44 | 52 |
|
45 | | - protected override void Seed(TestDbContext context) |
| 53 | + protected override void Seed(MyDbContext context) |
46 | 54 | { |
47 | 55 | context.Set<Player>().Add(new Player()); |
48 | 56 | } |
49 | 57 | } |
50 | 58 | ``` |
| 59 | + |
| 60 | +## Hints |
| 61 | +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 gettubg an exception when you try to run the console project. |
| 62 | +In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13. |
0 commit comments