ca9163d9
ca9163d9

Reputation: 29227

Accidentally deleted records in __MigrationHistory?

I accidentently deleted records in __MigrationHistory. I got the following error message when trying to Add-Migration for model changes.

Unable to generate an explicit migration because the following explicit migrations are pending: [201303101108238_InitialCreate, 201304020555457_xxx, 201305070251407_xxx, 201306090833462_xxx, 201306140437274_xxxx]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

It will get the following error if running Update-Database.

There is already an object named 'TableName' in the database.

What I can do to make it work?

Upvotes: 6

Views: 3358

Answers (2)

Bartłomiej Mucha
Bartłomiej Mucha

Reputation: 2782

In addition to what Mark mentioned, you could also find migration file (the one that you delete from __MigrationHistory table) in Migration folder, and comment code inside methods Up and Down then run Update-database. It should work.

Upvotes: 4

Mark van Straten
Mark van Straten

Reputation: 9425

The __MigrationHistory table contains all the information for Entity Framework to determine which migrations are done. Based on the current list of migrations in your code it will apply the missing migrations.

If you have deleted this table without the database part you have a few options

  • delete complete database, let entity framework rebuild it from scratch (for dev ok, you will lose your data)
  • create a second database, apply all migrations which were already done on the original and copy-paste the missing contents of the __MigrationHistory table.

Upvotes: 6

Related Questions