Skip to content

Commit b010cfb

Browse files
committed
Introduced SqliteDropCreateDatabaseAlways and SqliteCreateDatabaseIfNotExists. Renamed SqliteContextInitializer to SqliteInitializerBase and modified the visibility to internal.
1 parent 9aabb8c commit b010cfb

File tree

7 files changed

+76
-48
lines changed

7 files changed

+76
-48
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SQLite CodeFirst
22
Creates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst.
33

4-
This Project ships a `SqliteContextInitializer` which creates a new SQLite Database, based on your model/code.
4+
This Project ships several `IDbInitializer` which creates a new SQLite Database, based on your model/code.
55
I started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub).
66

77
Currently the following is supported:
@@ -13,38 +13,38 @@ Currently the following is supported:
1313
- Auto increment (An int PrimaryKey will automatically be incremented)
1414

1515
I tried to write the code in a extensible way.
16-
The logic is devided into two main parts. Builder and Statement.
16+
The logic is divided into two main parts. Builder and Statement.
1717
The Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code.
1818
The structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html).
1919
You will find an extensive usage of the composite pattern.
2020

2121
## 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`.
22+
If you want to let the Entity Framework create the database, if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists` as your `IDbInitializer`.
2323
```csharp
2424
public class MyDbContext : DbContext
2525
{
26-
public TestDbContext()
26+
public MyDbContext()
2727
: base("ConnectionStringName") { }
2828

2929
protected override void OnModelCreating(DbModelBuilder modelBuilder)
3030
{
31-
var sqliteConnectionInitializer = new SqliteContextInitializer<TestDbContext>(
31+
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>(
3232
Database.Connection.ConnectionString, modelBuilder);
3333
Database.SetInitializer(sqliteConnectionInitializer);
3434
}
3535
}
3636
```
3737

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.
38+
In a more advanced scenario you may want to populate some core- or test-data after the database was created.
39+
To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists` and override the `Seed(MyDbContext context)` function.
4040
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.
4141
```csharp
42-
public class TestDbContextInitializer : SqliteContextInitializer<TestDbContext>
42+
public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
4343
{
44-
public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder)
44+
public MyDbContextInitializer(string connectionString, DbModelBuilder modelBuilder)
4545
: base(connectionString, modelBuilder) { }
4646

47-
protected override void Seed(TestDbContext context)
47+
protected override void Seed(MyDbContext context)
4848
{
4949
context.Set<Player>().Add(new Player());
5050
}

SQLite.CodeFirst.Console/FootballDbContext.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
2121
ConfigurePlayerEntity(modelBuilder);
2222

2323
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
24+
// Workaround to support the [Timestamp] annotation
25+
modelBuilder.Conventions.Remove<TimestampAttributeConvention>();
2426

25-
var sqliteConnectionInitializer = new TestDbContextInitializer(Database.Connection.ConnectionString, modelBuilder);
26-
Database.SetInitializer(sqliteConnectionInitializer);
27+
var initializer = new FootballDbInitializer(Database.Connection.ConnectionString, modelBuilder);
28+
Database.SetInitializer(initializer);
2729
}
2830

2931
private static void ConfigureTeamEntity(DbModelBuilder modelBuilder)
@@ -45,9 +47,9 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
4547
}
4648
}
4749

48-
public class TestDbContextInitializer : SqliteContextInitializer<FootballDbContext>
50+
public class FootballDbInitializer : SqliteDropCreateDatabaseAlwaysInitializerBase<FootballDbContext>
4951
{
50-
public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder)
52+
public FootballDbInitializer(string connectionString, DbModelBuilder modelBuilder)
5153
: base(connectionString, modelBuilder) { }
5254

5355
protected override void Seed(FootballDbContext context)

SQLite.CodeFirst.Console/Program.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.IO;
2-
using SQLite.CodeFirst.Console.Entity;
1+
using SQLite.CodeFirst.Console.Entity;
32

43
namespace SQLite.CodeFirst.Console
54
{
@@ -9,27 +8,13 @@ static void Main()
98
{
109
System.Console.WriteLine("Starting Demo Application");
1110

12-
ClearExistingDbFile();
13-
1411
var context = CreateAndSeedDatabase();
1512

1613
DisplaySeededData(context);
1714

1815
PressEnterToExit();
1916
}
2017

21-
private static void ClearExistingDbFile()
22-
{
23-
System.Console.WriteLine("Check for existing db file.");
24-
const string SqliteFilePath = @".\footballDb.sqlite";
25-
if (File.Exists(SqliteFilePath))
26-
{
27-
System.Console.WriteLine("Delete existing db file.");
28-
File.Delete(SqliteFilePath);
29-
}
30-
System.Console.WriteLine();
31-
}
32-
3318
private static FootballDbContext CreateAndSeedDatabase()
3419
{
3520
System.Console.WriteLine("Create and seed the database.");

SQLite.CodeFirst/SQLite.CodeFirst.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
<Compile Include="Builder\ForeignKeyStatementBuilder.cs" />
5454
<Compile Include="Builder\CreateDatabaseStatementBuilder.cs" />
5555
<Compile Include="Builder\PrimaryKeyStatementBuilder.cs" />
56+
<Compile Include="SqliteCreateDatabaseIfNotExists.cs" />
57+
<Compile Include="SqliteDropCreateDatabaseAlways.cs" />
5658
<Compile Include="Statement\ColumnConstraint\MaxLengthConstraint.cs" />
5759
<Compile Include="Statement\PrimaryKeyStatement.cs" />
5860
<Compile Include="Properties\AssemblyInfo.cs" />
5961
<Compile Include="SqliteConnectionStringParser.cs" />
60-
<Compile Include="SqliteContextInitializer.cs" />
62+
<Compile Include="SqliteInitializerBase.cs" />
6163
<Compile Include="Builder\IStatementBuilder.cs" />
6264
<Compile Include="SqliteDatabaseCreator.cs" />
6365
<Compile Include="Statement\ColumnConstraint\ColumnConstraintCollection.cs" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Data.Entity;
2+
using System.IO;
3+
4+
namespace SQLite.CodeFirst
5+
{
6+
public class SqliteCreateDatabaseIfNotExists<TContext> : SqliteInitializerBase<TContext>
7+
where TContext : DbContext
8+
{
9+
public SqliteCreateDatabaseIfNotExists(string connectionString, DbModelBuilder modelBuilder)
10+
: base(connectionString, modelBuilder) { }
11+
12+
public override void InitializeDatabase(TContext context)
13+
{
14+
bool dbExists = File.Exists(DatabaseFilePath);
15+
if (dbExists)
16+
{
17+
return;
18+
}
19+
20+
base.InitializeDatabase(context);
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Data.Entity;
2+
using System.IO;
3+
4+
namespace SQLite.CodeFirst
5+
{
6+
public class SqliteDropCreateDatabaseAlwaysInitializerBase<TContext> : SqliteInitializerBase<TContext>
7+
where TContext : DbContext
8+
{
9+
public SqliteDropCreateDatabaseAlwaysInitializerBase(string connectionString, DbModelBuilder modelBuilder)
10+
: base(connectionString, modelBuilder) { }
11+
12+
public override void InitializeDatabase(TContext context)
13+
{
14+
bool dbExists = File.Exists(DatabaseFilePath);
15+
if (dbExists)
16+
{
17+
File.Delete(DatabaseFilePath);
18+
}
19+
20+
base.InitializeDatabase(context);
21+
}
22+
}
23+
}

SQLite.CodeFirst/SqliteContextInitializer.cs renamed to SQLite.CodeFirst/SqliteInitializerBase.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
using System;
22
using System.Data.Entity;
3-
using System.IO;
43

54
namespace SQLite.CodeFirst
65
{
7-
public class SqliteContextInitializer<TDbContext> : IDatabaseInitializer<TDbContext>
8-
where TDbContext : DbContext
6+
public abstract class SqliteInitializerBase<TContext> : IDatabaseInitializer<TContext>
7+
where TContext : DbContext
98
{
10-
protected readonly bool dbExists;
11-
protected readonly DbModelBuilder modelBuilder;
9+
protected readonly DbModelBuilder ModelBuilder;
10+
protected readonly string DatabaseFilePath;
1211

13-
public SqliteContextInitializer(string connectionString, DbModelBuilder modelBuilder)
12+
protected SqliteInitializerBase(string connectionString, DbModelBuilder modelBuilder)
1413
{
15-
string path = SqliteConnectionStringParser.GetDataSource(connectionString);
16-
dbExists = File.Exists(path);
17-
this.modelBuilder = modelBuilder;
14+
DatabaseFilePath = SqliteConnectionStringParser.GetDataSource(connectionString);
15+
ModelBuilder = modelBuilder;
1816
}
1917

20-
public virtual void InitializeDatabase(TDbContext context)
18+
public virtual void InitializeDatabase(TContext context)
2119
{
22-
if (dbExists)
23-
{
24-
return;
25-
}
26-
27-
var model = modelBuilder.Build(context.Database.Connection);
20+
var model = ModelBuilder.Build(context.Database.Connection);
2821

2922
using (var transaction = context.Database.BeginTransaction())
3023
{
@@ -57,6 +50,6 @@ public virtual void InitializeDatabase(TDbContext context)
5750
}
5851
}
5952

60-
protected virtual void Seed(TDbContext context) { }
53+
protected virtual void Seed(TContext context) { }
6154
}
6255
}

0 commit comments

Comments
 (0)