1

I need to move some data from a database to a web service. So I need to call the web service during a migration and upload existing data before the containing table is dropped. If data upload fails, the table must not be dropped. Also I would like to remove the data from the service in case when an exception occurs during other steps of this migration.

If I place the web service call within the Up method, the web service is called before the migration and the migration will not run if the service call throws. However, I do not see where can I put the rollback code. The statements in the Up method only enqueue operations to be executed and do not perform any database checks, so adding a catch there won't help.

The only possible solution I see now is to create my own migration runner rather than use migrate.exe and run migrations one by one:

var migrator = new DbMigrator(new MyMigrationConfiguration());
var pendingMigrations = migrator.GetPendingMigrations();
foreach (var migration in pendingMigrations)
{
    try
    {
        migrator.Update(migration);
    }
    catch (SqlException)
    {
        if (migration == "MoveDataToWebService")
        {
            // Delete data from service
        }

        throw;
    }
}

Are there any built-in ways to handle migration failures, so I can run migrations from Visual Studio or migrate.exe? Or maybe my approach to the original task is wrong and I should use something else than EF migrations to move the data?

Dark Daskin
  • 1,308
  • 1
  • 13
  • 22
  • Do you use *Package Manager Console*? – Denis Bubnov Dec 16 '15 at 15:10
  • You do something like this: http://stackoverflow.com/questions/32014118/can-entity-framework-6-migrations-include-a-transaction-around-scripts – Steve Greene Dec 16 '15 at 17:00
  • 1
    Why not making it into multiple steps? Instead of dropping table in migration just rename it, then do your web service sync, and then drop the table with new migration in future, when you are sure data is good – Red Dec 16 '15 at 21:04

0 Answers0