-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathSqliteDatabaseCreator.cs
40 lines (36 loc) · 1.48 KB
/
SqliteDatabaseCreator.cs
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
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Diagnostics;
namespace SQLite.CodeFirst
{
/// <summary>
/// Creates a SQLite-Database based on a Entity Framework <see cref="Database"/> and <see cref="DbModel"/>.
/// This creator can be used standalone or within an initializer.
/// <remark>
/// The generated DDL-SQL will be executed together as one statement.
/// If there is a open transaction on the Database, the statement will be executed within this transaction.
/// Otherwise it will be executed within a own transaction. In anyway the atomicity is guaranteed.
/// </remark>
/// </summary>
public class SqliteDatabaseCreator : IDatabaseCreator
{
public SqliteDatabaseCreator(Collation defaultCollation = null)
{
DefaultCollation = defaultCollation;
}
public Collation DefaultCollation { get; }
/// <summary>
/// Creates the SQLite-Database.
/// </summary>
public void Create(Database db, DbModel model)
{
if (db == null) throw new ArgumentNullException("db");
if (model == null) throw new ArgumentNullException("model");
var sqliteSqlGenerator = new SqliteSqlGenerator(DefaultCollation);
string sql = sqliteSqlGenerator.Generate(model.StoreModel);
Debug.Write(sql);
db.ExecuteSqlCommand(TransactionalBehavior.EnsureTransaction, sql);
}
}
}