0

I am running code first migrations. (EF 4.3.1)

I am also running Miniprofiler.

I run my code first migrations through code on App_Start.

My code looks like this:

public static int IsMigrating = 0;
    private static void UpdateDatabase()
    {
        try
        {
            if (0 == System.Threading.Interlocked.Exchange(ref IsMigrating, 1))
            {
                try
                {
                    // Automatically migrate database to catch up.
                    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Checking db for pending migrations.")));
                    var dbMigrator = new DbMigrator(new Ninja.Data.Migrations.Configuration());
                    var pendingMigrations = string.Join(", ", dbMigrator.GetPendingMigrations().ToArray());
                    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The database needs these code updates: " + pendingMigrations)));
                    dbMigrator.Update();
                    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Done upgrading database.")));
                }
                finally
                {
                    System.Threading.Interlocked.Exchange(ref IsMigrating, 0);
                }
            }
        }
        catch (System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException ex)
        {
            Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
        }
        catch (Exception ex)
        {
            Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
        }
    }

The problem is that my DbUpdate is about to get called and then my app throws an exception which I think comes from the app on the first web page request.

saying:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

The problem is that I think my homepage is firing the dbcontext and this error before my dbupdate has finished.

How would you go about solving this?

Should I make the context wait using locks etc or is there an easier way?

More interestingly, If i start and stop the app a few times the db changes are pushed and the error goes away...

So I need to find a way to have the first request to the database on App_Start wait for the migrations to happen.

Thoughts?

4
  • Are you running your method asynchronously? If you call it synchronously from App_Start your page code should not be executed until App_Start finish. Jul 29, 2012 at 16:10
  • No not asynch. I will profile the code tonight and get some more info.
    – Doug
    Jul 30, 2012 at 4:24
  • are you running the App_Start code on PreApplicationStartMethod or PostApplicationStartMethod? I believe miniprofiler should be prestart and upgrading db should be poststart
    – Betty
    Jul 31, 2012 at 4:08
  • This is correct, which is why the problem is occurring - Miniprofiler is loading a model that doesnt match the db yet
    – Doug
    Jul 31, 2012 at 12:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.