Home > Enterprise >  "Update-Database" command does not update to the latest version of Migration
"Update-Database" command does not update to the latest version of Migration

Time:12-13

Target Migration name is set to CreateNewProperty but, the migration getting executed is Base

I am trying to use Migrations in my project but when trying to update the database, only the base migration is applied.

I have a total of two migrations.

  1. The Base migration(initialy created)
  2. CreateNewProperty(Change includes adding a new property to one of the tables)

I see Enable-Migration mentioned in the Microsoft Documentation but it seems to have gone obsolete.

In addition to this, I also a mention of using the following in the Context class to enable latest migrations, but SetInitialier() method is no longer available:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, EF6Console.Migrations.Configuration>());

When I try to remove all migrations and bring it to the initial state by executing:

update-databse 0 it fails with the following error: ALTER TABLE DROP COLUMN failed because column 'test' does not exist in table 'Item'.

which is basically the update done in the CreateNewproperty migration and this error is expected as this migration was never applied. Why is EF trying to downgrade a migration, when it did not apply it in the first place.

CodePudding user response:

Use below command to Migrate to a Specific Version (Including Downgrade)

update-database -targetmigration:MigrationName

More information visit https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/#migrate-to-a-specific-version-including-downgrade

CodePudding user response:

Is it possible that your snapshot has become messed up somehow? You could try deleting that and then create a new migration (one that you could throw away) to rebuild it. Then try running update-database again.

  • Related