Skip to content

Commit d542a49

Browse files
committed
Merge pull request #51 from msallin/Issue-48
Fix for #48 and documentation (#25).
2 parents 28107bb + bf98760 commit d542a49

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

SQLite.CodeFirst/SqliteCreateDatabaseIfNotExists.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,59 @@
33

44
namespace SQLite.CodeFirst
55
{
6+
/// <summary>
7+
/// An implementation of <see cref="IDatabaseInitializer{TContext}"/> that will recreate and optionally re-seed the
8+
/// database only if the database does not exist. To seed the database, create a derived class and override the Seed method.
9+
/// </summary>
10+
/// <typeparam name="TContext">The type of the context.</typeparam>
611
public class SqliteCreateDatabaseIfNotExists<TContext> : SqliteInitializerBase<TContext>
712
where TContext : DbContext
813
{
14+
private readonly bool nullByteFileMeansNotExisting;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
18+
/// </summary>
19+
/// <param name="modelBuilder">The model builder.</param>
920
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder)
10-
: base(modelBuilder) { }
21+
: this(modelBuilder, false)
22+
{ }
1123

24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
26+
/// </summary>
27+
/// <param name="modelBuilder">The model builder.</param>
28+
/// <param name="nullByteFileMeansNotExisting">if set to <c>true</c> a null byte database file is treated like the database does not exist.</param>
29+
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting)
30+
: base(modelBuilder)
31+
{
32+
this.nullByteFileMeansNotExisting = nullByteFileMeansNotExisting;
33+
}
34+
35+
/// <summary>
36+
/// Initialize the database for the given context.
37+
/// Generates the SQLite-DDL from the model and executs it against the database.
38+
/// After that the <see cref="Seed" /> method is executed.
39+
/// All actions are be executed in transactions.
40+
/// </summary>
41+
/// <param name="context">The context.</param>
1242
public override void InitializeDatabase(TContext context)
1343
{
14-
string databseFilePath = GetDatabasePathFromContext(context);
44+
string databaseFilePath = GetDatabasePathFromContext(context);
45+
46+
var fileInfo = new FileInfo(databaseFilePath);
47+
48+
bool exists;
49+
if (nullByteFileMeansNotExisting)
50+
{
51+
exists = fileInfo.Exists && fileInfo.Length != 0;
52+
}
53+
else
54+
{
55+
exists = fileInfo.Exists;
56+
}
1557

16-
bool dbExists = File.Exists(databseFilePath);
17-
if (dbExists)
58+
if (exists)
1859
{
1960
return;
2061
}

SQLite.CodeFirst/SqliteDatabaseCreator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
namespace SQLite.CodeFirst
77
{
8+
/// <summary>
9+
/// Creates a SQLite-Database based on a Entity Framework <see cref="Database"/> and <see cref="DbModel"/>.
10+
/// This creator can be used standalone or within an initializer.
11+
/// </summary>
812
public class SqliteDatabaseCreator
913
{
1014
private readonly Database db;
@@ -16,6 +20,9 @@ public SqliteDatabaseCreator(Database db, DbModel model)
1620
this.model = model;
1721
}
1822

23+
/// <summary>
24+
/// Creates the SQLite-Database.
25+
/// </summary>
1926
public void Create()
2027
{
2128
IStatementBuilder<CreateDatabaseStatement> statementBuilder = new CreateDatabaseStatementBuilder(model.StoreModel);

SQLite.CodeFirst/SqliteDropCreateDatabaseAlways.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33

44
namespace SQLite.CodeFirst
55
{
6+
/// <summary>
7+
/// An implementation of <see cref="IDatabaseInitializer{TContext}"/> that will always recreate and optionally re-seed the
8+
/// database the first time that a context is used in the app domain. To seed the database, create a derived class and override the Seed method.
9+
/// </summary>
10+
/// <typeparam name="TContext">The type of the context.</typeparam>
611
public class SqliteDropCreateDatabaseAlways<TContext> : SqliteInitializerBase<TContext>
712
where TContext : DbContext
813
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="SqliteDropCreateDatabaseAlways{TContext}"/> class.
16+
/// </summary>
17+
/// <param name="modelBuilder">The model builder.</param>
918
public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder)
10-
: base(modelBuilder) { }
19+
: base(modelBuilder)
20+
{ }
1121

22+
/// <summary>
23+
/// Initialize the database for the given context.
24+
/// Generates the SQLite-DDL from the model and executs it against the database.
25+
/// After that the <see cref="Seed" /> method is executed.
26+
/// All actions are be executed in transactions.
27+
/// </summary>
28+
/// <param name="context">The context.</param>
1229
public override void InitializeDatabase(TContext context)
1330
{
1431
string databseFilePath = GetDatabasePathFromContext(context);

0 commit comments

Comments
 (0)