Created
October 26, 2016 21:26
-
-
Save derekgreer/7d748f19bef129f9690dc8569f597902 to your computer and use it in GitHub Desktop.
InMemorySqliteDatabaseInitializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InMemorySqliteDatabaseInitializer<TContext> : SqliteInitializerBase<TContext> | |
where TContext : DbContext | |
{ | |
public InMemorySqliteDatabaseInitializer(DbModelBuilder modelBuilder) | |
: base(modelBuilder) | |
{ | |
} | |
public override void InitializeDatabase(TContext context) | |
{ | |
InitializeDatabaseInMemory(context); | |
} | |
void InitializeDatabaseInMemory(TContext context) | |
{ | |
var model = ModelBuilder.Build(context.Database.Connection); | |
using (var transaction = context.Database.BeginTransaction()) | |
{ | |
try | |
{ | |
var sqliteDatabaseCreator = new SqliteDatabaseCreator(); | |
sqliteDatabaseCreator.Create(context.Database, model); | |
transaction.Commit(); | |
} | |
catch (Exception ex) | |
{ | |
transaction.Rollback(); | |
throw; | |
} | |
} | |
using (var transaction = context.Database.BeginTransaction()) | |
{ | |
try | |
{ | |
Seed(context); | |
context.SaveChanges(); | |
transaction.Commit(); | |
} | |
catch (Exception) | |
{ | |
transaction.Rollback(); | |
throw; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment