Reputation: 1026
I have an custom IDatabaseInitializer
in Entity Framework 6 and noticed strange thing. When calling DbMigrator.Update
method, if the migration fails the transaction, it is not rolled back thus leaving database in inconsistent state where you can no longer downgrade.
Here is the migration code i am using:
var migrator = new DbMigrator(_configuration);
var pendingMigrations = migrator.GetPendingMigrations();
if (migrator.GetPendingMigrations().Any())
{
migrator.Update();
}
Should I simply do something like this:
using (var trx = context.Database.BeginTransaction())
{
try
{
var migrator = new DbMigrator(_configuration);
var pendingMigrations = migrator.GetPendingMigrations();
if (migrator.GetPendingMigrations().Any())
{
migrator.Update();
}
trx.Commit();
}catch
{
trx.Rollback();
throw;
}
}
As I understand this should work, just cant figure why DbMigrator
doesn't do this internally.
Upvotes: 3
Views: 179
Reputation: 1026
It seems that occurs due to database engines such as MYSQL i was using not supporting transactional ALTER and would do implicit commit.
http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html
Upvotes: 1