The first EF code migrations do not start after deployment to Azure

I have two folders for my migrations (AuthContext and UserProfileContext), each of which has its own migration and some custom sql for the subsequent start of data migration and something else.

This works great when using the package manager console. I

  • Recovery from production
  • Run Update-Database -ConfigurationTypeName MigrationsAuth.Configuration
  • Run Update-Database -ConfigurationTypeName Migrations.UserProfile.Configuration

Then everyone in the new database is very happy, migrations performed data shuffled where necessary.

I tried to check migrations for publication using:

  • Restore production to dev database
  • A single connection string (all contexts use the same thing), pointing to the dev database
  • Publish to Azure Website
  • The checkbox "Primary application migrations" is checked
  • Selected that single connection string

It is good that it is published perfectly; however, when I went to look at the database, nothing happened! He did not create the necessary tables, columns, or data movements.

TL; DR; First code migrations do not start after publishing to Azure

Update 1 I tried any combination of the following: only one connection string, so I assume that it is not a problem, but the migration is being checked. enter image description here

When publishing, the api is executed, but no changes are made to the database. I thought that maybe I needed to click first, but I just get random errors when I try to use api (which now, of course, relies on the new database setup) and the database is still not changing.

I saw a couple of links about the need to add something to my Startup class, but I'm not sure how to proceed.

Update 2 I solved one problem by adding "Persist Security Info = True" to the connection string. Now it actually connects to the database and calls my API; however, migration is not performed. I connected the debugger to the Azure dev environment and went through ... in my first database call, it enters the Configuration class for the migration in question, then barfs and I can not track the error.

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

Update 3

Well, I dug up and the first time he got into the database, we are mistaken. Yes, that makes sense as the model has changed, but I have migration in place, it’s turned on and verified! Again, it works great when running Update-Database from the package manager console, but not when using Execute Code First Migrations while publishing to Azure

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

Update 4 Ok, I found a root here. VS sets up an additional web.config file for databaseInitializer for only one of the contexts of my database, the one that is not mentioned actually gets the first from my application.

So now I have to figure out how to get it to include several contexts or to combine all my things into one context.

+6
source share
2 answers

Solved! Summarize the solution for posterity:

Enable Code First Migrations only allows you to use them for one connection base line for each flag, regardless of how many contexts the migration against this connection base line has. Therefore, in my case, I broke two questions into two different connection strings.

Then I hit other errors and determined that if you change the base connection string to the model substitution asp identifier, you need to enable (one time to publish) an additional flag base ("AuthContext", throwIfV1Schema: false)

+3
source

The answer to this post is not very detailed.

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

I will roughly describe the steps I should have taken below:

Step 1 Add your connection strings to your dbContexts, in my situation they were 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 (start OWIN)

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

Source: https://habr.com/ru/post/987350/


All Articles