EF Code first migrations not running after deploy to Azure

azure ef-code-first ef-migrations entity-framework

Question

I have two folders for my migrations (AuthContext and UserProfileContext), each has their own migration and some custom sql to run afterwards for data migrations and whatnot.

This works fine when using package manager console. I

  1. Restore from production
  2. Run Update-Database -ConfigurationTypeName Migrations.Auth.Configuration
  3. Run Update-Database -ConfigurationTypeName Migrations.UserProfile.Configuration

Then everything is very happy in the new database, migrations executed data shuffled where it needs to.

I tried to test out the migrations on publish piece by:

  1. Restore production on dev database
  2. Single connection string (all contexts use the same) pointed to dev database
  3. Publish to azure web site
  4. Checked the box for Apply Code First Migrations
  5. Selected that single connection string

Okay it published fine; however, when I went to look at the database, nothing happened! It did not create the necessary tables, columns, or data moves.

TLDR; Code first migrations are not running after publish to Azure

Update 1 I've tried any combination of the below: only one single connection string so I'm guessing that's not the issue, and execute migrations is checked. enter image description here

On publish the api runs but no database changes are made. I thought perhaps I needed to hit it first but I just get random errors when I try to use the api (which now of course relies on the new database setup), and the database is still not changed.

I've seen a couple references out there about needing to add something to my Startup class but I'm not sure how to proceed.

Update 2 I solved one issue by added "Persist Security Info=True" to my connection string. Now it actually connects to the database and calls my API; however, no migrations are running. I attached debugger to Azure dev environment and stepped through... on my first database call it steps into the Configuration class for the Migration in question, then barfs and I can't track down the error.

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    MigrationsDirectory = @"Migrations\Auth";
    ContextKey = "AuthContext";
}

Update 3

Okay, dug down and the first time it hits the database we're erroring. Yes this makes sense since the model has changed, but I have migrations in place, enabled, and checked! Again, it works fine when running "Update-Database" from package manager console, but not when using Execute Code First Migrations during publish to Azure

The model backing the 'AuthContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Update 4 Okay I found the root issue here. VS is setting up the additional web.config attrib for databaseInitializer on only one of my database contexts, the one not mentioned is in fact hit first from my app.

So now I have to figure out how to get it to include multiple contexts, or, combine all of my stuff into a single context.

1
7
5/16/2015 11:29:38 PM

Accepted Answer

Solved! To summarize the solution for posterity:

Enable Code First Migrations only enables them for one base connection string per checkbox checked, regardless of how many contexts have migrations against that base connection string. So in my case I broke out the two in question into two different connection strings.

Then I was hitting other errors and identified that if you're changing the base connection string to the model backing asp identity you need to include (one time publish) the additional flag base("AuthContext" , throwIfV1Schema: false)

4
5/17/2015 12:48:49 PM

Popular Answer

The answer to this post is not very detailed.

This article explains what I had to do to fix a similar problem to this: https://blogs.msdn.microsoft.com/webdev/2014/04/08/ef-code-first-migrations-deployment-to-an-azure-cloud-service/

I'll roughly describe the steps I had to take below:

Step 1 Add your connection strings to your dbContexts, in my situation, they were both the same.

enter image description here

Step 2 Add this to your web.config

 <appSettings>
    <add key="MigrateDatabaseToLatestVersion" value="true"/>
  </appSettings>

Step 3 And add this to the bottom of your global.asax.cs / Startup.cs(OWIN startup)

    var configuration = new Migrations.Configuration();
    var migrator = new DbMigrator(configuration);
    migrator.Update();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow