Dan Faber
Dan Faber

Reputation: 23

dbo.__MigrationHistory table not being updated when publishing from Visual Studio

I am having an issue with the dbo.__MigrationHistory table not being updated with new database migrations, BUT the actual database itself is correctly updated with relevant changes. This means the site won't load due to it spotting a change in the context - and I have to manually insert the new changes into the dbo.__MigrationHistory table to make it load.

When I publish to Azure from Visual Studio everything works fine in my PRODUCTION site. However, I have the issue only on my TEST site (which has a different publish profile).

I have checked the EXECUTE CODE FIRST MIGRATIONS box in the publish profile on both the TEST and PRODUCTION publish profiles. Indeed, both publish profiles appear to be identical except for pushing to a different site.

In case it helps - whenever this happens my localdb SqlServer database also becomes unattached during the publish. So on my local PC I have to then go back and reattach the .mdf database file in SQL server management studio.

Any help / advice you can offer would be amazing.

Upvotes: 1

Views: 773

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 5294

I've had a number of occasions where Entity Framework migrations have left the state of migrations in an unusable state. Usually this happens after a large number of migrations have been applied from different developers and get stuck to where we can't update a database with new migrations or roll back.

It's simply easier to delete the migrations and start with a clean state from the current schema.

If you go the Route of resetting your migrations, make sure you back up your code and make known good backups of your database.

In summary, the steps to do this are:

  1. Remove the _MigrationHistory table from the Database
  2. Remove the individual migration files in your project's Migrations folder
  3. Enable-Migrations in Package Manager Console
  4. Add-migration Initial in Package Manager Console
  5. Comment out the code inside of the Up method in the Initial Migration
  6. Update-database in Package Manager Console(does nothing but creates Migration Entry)
  7. Remove comments in the Initial method

This is not a ideal work around but this will resolve the your issue.

P.S.- This usually occurs when somebody update DB from local machine and it goes out of sync.

Upvotes: 1

Related Questions