Skip to content

Instantly share code, notes, and snippets.

@derekgreer
Created October 26, 2016 21:26
Show Gist options
  • Save derekgreer/7d748f19bef129f9690dc8569f597902 to your computer and use it in GitHub Desktop.
Save derekgreer/7d748f19bef129f9690dc8569f597902 to your computer and use it in GitHub Desktop.
InMemorySqliteDatabaseInitializer
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