Skip to content

Commit fbb5b8f

Browse files
author
Marc Sallin
committed
#16: Updates the Readme.
1 parent 9aefc21 commit fbb5b8f

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ You can use the SQLite CodeFirst in projects that target the following framework
4242
- .NET 4.6.1 (use net45)
4343

4444
## How to use
45+
The functionality is exposed by using implementations of the `IDbInitializer<>` interface.
46+
Depending on your need, you can choose from the following initializers:
47+
- SqliteCreateDatabaseIfNotExists
48+
- SqliteDropCreateDatabaseAlways
49+
- SqliteDropCreateDatabaseWhenModelChanges
50+
51+
If you want to have more control, you can use the `SqliteDatabaseCreator` (implements `IDatabaseCreator`) which lets you control the creation of the SQLite database.
52+
Or for even more control, use the `SqliteSqlGenerator` (implements `ISqlGenerator`), which lets you generate the SQL code based on your `EdmModel`.
53+
4554
When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
55+
56+
### Initializer Sample
4657
```csharp
4758
public class MyDbContext : DbContext
4859
{
@@ -56,9 +67,12 @@ public class MyDbContext : DbContext
5667
}
5768
}
5869
```
70+
Notice that the `SqliteDropCreateDatabaseWhenModelChanges<>` initializer will create a additional table in your database.
71+
This table is used to store some information to detect model changes. If you want to use a own entity/table you can implement the
72+
`IHistory` interface and pass the type of your entity as parameter in the to the constructor from the initializer.
5973

6074
In a more advanced scenario, you may want to populate some core- or test-data after the database was created.
61-
To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function.
75+
To do this, inherit from `SqliteDropCreateDatabaseAlways<>`, `SqliteCreateDatabaseIfNotExists<>` or `SqliteDropCreateDatabaseWhenModelChanges<>` and override the `Seed(MyDbContext context)` function.
6276
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.
6377
```csharp
6478
public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
@@ -73,6 +87,32 @@ public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext
7387
}
7488
```
7589

90+
### SqliteDatabaseCreator Sample
91+
```csharp
92+
public class MyContext : DbContext
93+
{
94+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
95+
{
96+
var model = modelBuilder.Build(Database.Connection);
97+
IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();
98+
sqliteDatabaseCreator.Create(Database, model);
99+
}
100+
}
101+
```
102+
103+
### SqliteSqlGenerator Sample
104+
```csharp
105+
public class MyContext : DbContext
106+
{
107+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
108+
{
109+
var model = modelBuilder.Build(Database.Connection);
110+
ISqlGenerator sqlGenerator = new SqliteSqlGenerator();
111+
string sql = sqlGenerator.Generate(model.StoreModel);
112+
}
113+
}
114+
```
115+
76116
## Hints
77117
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.
78118
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.

0 commit comments

Comments
 (0)