Skip to content

Commit 5301584

Browse files
author
Marc Sallin
committed
Update README.md
1 parent fabe113 commit 5301584

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
# 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.
43

54
This Project ships a "SqliteContextInitializer" which creates a new SQLite Database, based on your model/code.
65
I started with the code from flaub: https://gist.github.com/flaub/1968486e1b3f2b9fddaf
76

87
Currently the following is supported:
98
- Tables from classes
109
- Columns from properties
11-
- 1-n relationships
10+
- ForeignKey constraint (1-n relationships)
1211
- Cascade on delete
13-
- PrimaryKey constraint
12+
- PrimaryKey constraint (An int PrimaryKey will automatically be incremented)
1413
- Not Null constraint
1514

1615
I tried to write the code in a extensible way.
1716
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

Comments
 (0)