question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DbMigrator Update() issue after updating to EF 6.2

See original GitHub issue

I have been getting the following issue after updating my project from EF 6.1.3 to 6.2. This is an Asp.Net application where I keep my Code First models and Db context in a separate project.

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.

Following is the code where I get the exception, which is in Startup.Auth.cs file.

// Entity Framework Db Migration var efMigrationSettings = new Domain.Services.Migrations.Configuration(); var efMigrator = new DbMigrator(efMigrationSettings); efMigrator.Update();

I do not get any error when I tried manually updating the db from the “Package Manager Console” using “update-database” command, and yet when I run the app using F5 I still get this error.

But when I downgraded EF to 6.1.3 everything started working perfectly.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Bronzato1commented, Aug 13, 2019

Many thanks to @mark-trumed for commenting. I was in the exact same situation.

0reactions
shaojuncommented, Oct 16, 2019

I had this problem maybe it’s too late to report here, my code:

    var migrator = new DbMigrator(
                new DbMigrationsConfiguration()
                {
                    ContextType = typeof(DefaultAppDbContext),
                    MigrationsAssembly = Assembly.GetExecutingAssembly(),
                    AutomaticMigrationsEnabled = false,
                }
                );
    logger.Info("PendingMigrations: " + (migrator.GetPendingMigrations()?.Aggregate("", (acc, n) => acc + ", " + n) ?? ""));
    migrator.Update();

the exception say:

System.Data.Entity.Migrations.Infrastructure.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. at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.<Update>b__d() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()

if I change the AutomaticMigrationsEnabled to true, the exception say:

System.Data.Entity.Migrations.Infrastructure.MigrationsException: Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported. Please use code-based migrations for operations that affect the location of the migrations history system table. at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.<Update>b__d() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()

at last, if I do the EntityFramework\Add-Migration test in visual studio 2019 Package Manager Console, it always generate an empty migration record that means the Model and the migration history is aligned.

BTW, the above Netwonsoft.Json 11.0.2 explain is not my situation, I had its version of 12.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DbMigrator Update() issue after updating to EF 6.2 #391
Unable to update database to match the current model because there are pending changes and automatic migration is disabled.
Read more >
Error running code first migration update-database
I ran into this error when running db.Database.Initialize(true); . I discovered that my the console app was referencing EF v 6.0, ...
Read more >
DbMigrator.Update(String) Method (System.Data.Entity. ...
Updates the target database to a given migration.
Read more >
Code First Migrations - EF6
Generating migrations to keep track of changes you make to your EF model; Keep your database up to date with those changes. The...
Read more >
Entity Framework Core Migrations
EF Core Migrations automatically generates and executes the necessary SQL scripts to update the database schema, so you don't have to write ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found