|
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 | 12 | - Tables from classes (supported annotations: `Table`) |
9 | | -- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`) |
| 13 | +- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`, `NotMapped`, `DatabaseGenerated`) |
10 | 14 | - PrimaryKey constraint (`Key` annotation, key composites are supported) |
11 | 15 | - ForeignKey constraint (1-n relationships, support for 'Cascade on delete') |
12 | 16 | - Not Null constraint |
13 | 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. |
| 20 | +The logic is divided into two main parts. Builder and Statement. |
17 | 21 | The Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code. |
18 | 22 | The structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html). |
19 | 23 | You will find an extensive usage of the composite pattern. |
20 | 24 |
|
21 | 25 | ## How to use |
22 | | -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<>`. |
23 | 29 | ```csharp |
24 | 30 | public class MyDbContext : DbContext |
25 | 31 | { |
26 | | - public TestDbContext() |
| 32 | + public MyDbContext() |
27 | 33 | : base("ConnectionStringName") { } |
28 | 34 |
|
29 | 35 | protected override void OnModelCreating(DbModelBuilder modelBuilder) |
30 | 36 | { |
31 | | - var sqliteConnectionInitializer = new SqliteContextInitializer<TestDbContext>( |
| 37 | + var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>( |
32 | 38 | Database.Connection.ConnectionString, modelBuilder); |
33 | 39 | Database.SetInitializer(sqliteConnectionInitializer); |
34 | 40 | } |
35 | 41 | } |
36 | 42 | ``` |
37 | 43 |
|
38 | | -In a more advanced szenario you may want to populate some core- or test-data after the database was created. |
39 | | -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. |
40 | 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. |
41 | 47 | ```csharp |
42 | | -public class TestDbContextInitializer : SqliteContextInitializer<TestDbContext> |
| 48 | +public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext> |
43 | 49 | { |
44 | | - public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder) |
| 50 | + public MyDbContextInitializer(string connectionString, DbModelBuilder modelBuilder) |
45 | 51 | : base(connectionString, modelBuilder) { } |
46 | 52 |
|
47 | | - protected override void Seed(TestDbContext context) |
| 53 | + protected override void Seed(MyDbContext context) |
48 | 54 | { |
49 | 55 | context.Set<Player>().Add(new Player()); |
50 | 56 | } |
51 | 57 | } |
52 | 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