-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathSqliteCreateDatabaseIfNotExists.cs
More file actions
75 lines (67 loc) · 3.65 KB
/
Copy pathSqliteCreateDatabaseIfNotExists.cs
File metadata and controls
75 lines (67 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Data.Entity;
using System.IO;
using SQLite.CodeFirst.Utility;
namespace SQLite.CodeFirst
{
/// <summary>
/// An implementation of <see cref="IDatabaseInitializer{TContext}"/> that will recreate and optionally re-seed the
/// database only if the database does not exist. To seed the database, create a derived class and override the Seed method.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class SqliteCreateDatabaseIfNotExists<TContext> : SqliteInitializerBase<TContext>
where TContext : DbContext
{
private readonly bool nullByteFileMeansNotExisting;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder)
: this(modelBuilder, false, null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="defaultCollation">The default collation applied to all string columns. Explicit <see cref="CollateAttribute"/>s take precedence.</param>
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, Collation defaultCollation)
: this(modelBuilder, false, defaultCollation)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="nullByteFileMeansNotExisting">if set to <c>true</c> a null byte database file is treated like the database does not exist.</param>
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting)
: this(modelBuilder, nullByteFileMeansNotExisting, null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCreateDatabaseIfNotExists{TContext}"/> class.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="nullByteFileMeansNotExisting">if set to <c>true</c> a null byte database file is treated like the database does not exist.</param>
/// <param name="defaultCollation">The default collation applied to all string columns. Explicit <see cref="CollateAttribute"/>s take precedence.</param>
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder, bool nullByteFileMeansNotExisting, Collation defaultCollation)
: base(modelBuilder, defaultCollation)
{
this.nullByteFileMeansNotExisting = nullByteFileMeansNotExisting;
}
/// <summary>
/// Initialize the database for the given context.
/// Generates the SQLite-DDL from the model and executs it against the database.
/// After that the <see cref="Seed" /> method is executed.
/// All actions are be executed in transactions.
/// </summary>
/// <param name="context">The context.</param>
public override void InitializeDatabase(TContext context)
{
string databaseFilePath = GetDatabasePathFromContext(context);
bool exists = InMemoryAwareFile.Exists(databaseFilePath, nullByteFileMeansNotExisting);
if (exists)
{
return;
}
base.InitializeDatabase(context);
}
}
}