forked from msallin/SQLiteCodeFirst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqliteInitializerBase.cs
More file actions
116 lines (105 loc) · 4.64 KB
/
SqliteInitializerBase.cs
File metadata and controls
116 lines (105 loc) · 4.64 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using SQLite.CodeFirst.Convention;
using System.IO;
namespace SQLite.CodeFirst
{
/// <summary>
/// An basic implementation of the <see cref="IDatabaseInitializer{TContext}"/> interface.
/// This class provides common logic which can be used when writing an Sqlite-Initializer.
/// The logic provided is:
/// 1. Remove/Add specific Conventions
/// 2. Get the path to the database file
/// 3. Create a new SQLite-Database from the model (Code First)
/// 4. Seed data to the new created database
/// The following implementations are provided: <see cref="T:SQLite.CodeFirst.SqliteCreateDatabaseIfNotExists`1"/>, <see cref="T:SQlite.CodeFirst.SqliteDropCreateDatabaseAlways`1"/>.
/// </summary>
/// <typeparam name="TContext">The type of the context.</typeparam>
public abstract class SqliteInitializerBase<TContext> : IDatabaseInitializer<TContext>
where TContext : DbContext
{
private readonly DbModelBuilder modelBuilder;
protected SqliteInitializerBase(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
this.modelBuilder = modelBuilder;
// This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
modelBuilder.Conventions.Remove<TimestampAttributeConvention>();
// By default there is a 'ForeignKeyIndexConvention' but it can be removed.
// And there is no "Contains" and no way to enumerate the ConventionsCollection.
// So a try/catch will do the job.
try
{
// Place the own ForeinKeyIndexConvention right after the original.
// The own convention will rename the automatically created indicies by using the correct scheme.
modelBuilder.Conventions.AddAfter<ForeignKeyIndexConvention>(new SqliteForeignKeyIndexConvention());
}
catch (InvalidOperationException exception)
{
// Ignore it.
}
}
/// <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 virtual void InitializeDatabase(TContext context)
{
var model = modelBuilder.Build(context.Database.Connection);
var dbFile = GetDatabasePathFromContext(context);
var dbFileInfo = new FileInfo(dbFile);
dbFileInfo.Directory.Create();
using (var transaction = context.Database.BeginTransaction())
{
try
{
var sqliteDatabaseCreator = new SqliteDatabaseCreator(context.Database, model);
sqliteDatabaseCreator.Create();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
using (var transaction = context.Database.BeginTransaction())
{
try
{
Seed(context);
context.SaveChanges();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}
/// <summary>
/// Is executed right after the initialization <seealso cref="InitializeDatabase"/>.
/// Use this method to seed data into the empty database.
/// </summary>
/// <param name="context">The context.</param>
protected virtual void Seed(TContext context) { }
/// <summary>
/// Gets the database path file path from a <see cref="TContext"/>.
/// </summary>
/// <param name="context">The context to get the database file path from.</param>
/// <returns>The full path to the SQLite database file.</returns>
protected string GetDatabasePathFromContext(TContext context)
{
return SqliteConnectionStringParser.GetDataSource(context.Database.Connection.ConnectionString);
}
}
}