-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathSqliteDropCreateDatabaseAlways.cs
More file actions
57 lines (53 loc) · 2.51 KB
/
Copy pathSqliteDropCreateDatabaseAlways.cs
File metadata and controls
57 lines (53 loc) · 2.51 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
using System.Data.Entity;
using System.IO;
using SQLite.CodeFirst.Utility;
namespace SQLite.CodeFirst
{
/// <summary>
/// An implementation of <see cref="IDatabaseInitializer{TContext}"/> that will always recreate and optionally re-seed the
/// 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.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public class SqliteDropCreateDatabaseAlways<TContext> : SqliteInitializerBase<TContext>
where TContext : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="SqliteDropCreateDatabaseAlways{TContext}"/> class.
/// </summary>
/// <param name="modelBuilder">The model builder.</param>
public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder)
: this(modelBuilder, null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SqliteDropCreateDatabaseAlways{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 SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder, Collation defaultCollation)
: base(modelBuilder, defaultCollation)
{ }
/// <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 databseFilePath = GetDatabasePathFromContext(context);
bool exists = InMemoryAwareFile.Exists(databseFilePath);
if (exists)
{
FileAttributes? attributes = InMemoryAwareFile.GetFileAttributes(databseFilePath);
InMemoryAwareFile.Delete(databseFilePath);
base.InitializeDatabase(context);
InMemoryAwareFile.SetFileAttributes(databseFilePath, attributes);
}
else
{
base.InitializeDatabase(context);
}
}
}
}