Unable to update database to match the current model because there are pending changes

asp.net-mvc ef-code-first entity-framework

Question

I have a project built in visual studio 2013 environment with the Db built using EF 5 code first. I have had my APIs working fine for a long time but all of a sudden I started to get this error that says:

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.

when I try to reach the end points of my APIs. I tried adding a new migration and then updating the database but still got the error. I then drop my entire database and recreated with EF. The end points of my APIs started working fine but then again I started getting this error on the web page. I have automatic migration set to true in the configuration file. I really have no idea why this is happening over and over. It's getting me really frustrated. Here's the full stack trace of the error:

[AutomaticMigrationsDisabledException: 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.]
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +579 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +445
System.Data.Entity.Migrations.<>c__DisplayClassc.b__b() +13
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +12 YourTimeSite.Global.ApplyDatabaseMigrations() in c:\Users\Ahmed\Desktop\YourTimeSite\YourTimeSite\Global.asax.cs:55
YourTimeSite.Global.Application_Start(Object sender, EventArgs e) in c:\Users\Ahmed\Desktop\YourTimeSite\YourTimeSite\Global.asax.cs:32

[HttpException (0x80004005): 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.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9966013
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +352
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): 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.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9947380 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101

1
3
8/22/2018 11:06:29 AM

Accepted Answer

There's not really enough here to diagnose the root cause of the issue, but generally, you will only get that error if you're database is out of sync with your entity classes in some way. If you truly believe it is not, you can disable this exception from occurring by deleting the _MigrationHistory table from your production database. At that point, EF will treat the database as existing, and no longer prompt you to migrate it. Instead, you'll get exceptions only when it find unexpected/missing columns, or other SQL errors resulting from schema desynchronization. In some ways, that's better though, as if there actually is something off, you'll have better idea of exactly what is off, rather than being broadly told that you need to migrate.

However, removing the migration history table means that you will then be responsible for keeping it synced up if you do make changes to your entity classes. Generally, that's not a problem, anyways. It's a poor idea to run migrations against a production database, anyways, so this actually forces you to explicitly update the schema when necessary, hopefully using proper change management policies.

5
8/15/2016 3:42:08 PM

Popular Answer

I had a lot of trouble with this but the answer for me was to exclude intermediate database migration items that altered tables which were already created with those alterations. If I knew a bit more about what I am doing here, I think I would follow the advice above on the DbContext settings.

Running update-database produced the same errors, but:

  • adding an empty migration (via PM) and then update_database worked

add-migration EmptyMigration

Then run

update-database

At this point the migrations ran without error.

  • To wit, several migrations required exclusion from the project (not understanding how the CodeFirst project had already created these migrations and thus they were unneeded and therefore, "exclude"-able).
  • Project Migrations included intermediate steps that updated the table structure that EntityFramework had already applied (Entity Framework newbie- I am - working with CodeFirst examples from udemy - MVC5 course)
  • What began as "Unable to create explicit migration for..." (VS2019)
  • Then "No pending explicit migrations.Found invalid data while decoding."
  • Followed instructions to delete obj folder and then clear the cache from Nuget without any result.

  • Next, tried to delete and recreate the database with an empty database (I'm running developer version of SQL Server but Express would be the same) using SSMS. This part may or may not have been needed.

  • Brought the project up and noticed it was Net Framework 4.5 so updated to 4.7. Again, not sure if it was needed. The sample project was built for Community Edition 2013.

  • Restarted the project (VS2019)

  • Performed two steps above and everything is working now.


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