7
votes

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.

3
Have you take a look this post blogs.msdn.com/b/webdev/archive/2014/04/09/…, it looks like you might need add code to your Application_Start code of Global.asaxJ.W.
@J.W. Thanks, I'd seen that, it's specifically for Azure Cloud Service and I'm using Azure Website, for which supposedly you don't need to do anything extra...Joshua Ohana
I think you have to make sure the db migration code runs no matter what kind of project it is.J.W.
@J.W. Snippet from your linked article which indicates the opposite: "Another option is to set up Web.config transforms to make the same changes to the Web.config file that Visual Studio does when you click Execute Code First Migrations". Visual Studio should be setting up the necessary migrations during deployment, and in fact as evident by the Configuration class being called something is happening...Joshua Ohana
I see, you have multiple contexts, does it mean that you have multiple databases, that normally doesn't play well with EF.J.W.

3 Answers

10
votes

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();
4
votes

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)

0
votes

For anyone who has this issue and may have overlooked the following: be sure to check that you have correctly set the connection string in your Web.config file and/or Application settings on Azure. This includes DefaultConnection and DefaultConnection_DatabasePublish.

In our case the former was correct but the latter contained the wrong database instance because it had been carried over from an App Service clone operation. Therefore the wrong database was being migrated.