173

For some reason, my migrations appear to have been jumbled/corrupted/whatever. I'm at the point where I just want to start over, so is there a way to completely undo all migrations, erase the history, and delete the migration code, so I'm back to square one?

e.g.) PM> Disable-Migrations or Rollback-Migrations

I don't want to "update" to an original migration step (i.e. something like an InitialSchema target) because I can't find it anymore.

drzaus
  • 24,171
  • 16
  • 142
  • 201

5 Answers5

365

You can rollback to any migration by using:

Update-Database -TargetMigration:"MigrationName"

If you want to rollback all migrations you can use:

Update-Database -TargetMigration:0

or equivalent:

Update-Database -TargetMigration:$InitialDatabase 

In some cases you can also delete database and all migration classes.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 7
    as usual, you're the best. – drzaus Apr 24 '12 at 14:16
  • 2
    I don't understand, the OP *specifically* says "I don't want to "update" to an original migration step (i.e. something like an InitialSchema target) because I can't find it anymore." How does this answer do what he wants? What I would expect to have happen was that I would be at a state where I have to `enable-migrations` again. These are obviously helpful, but do they accomplish what the OP is asking for? (Please don't reply with "well, he accepted the answer." I'm trying to understand this, not be a smartass). – Michael Blackburn Dec 26 '12 at 05:22
  • @MichaelBlackburn: If you want to run `enable-migrations` again and you are developing database from scratch you just need to follow the last sentence: delete database and all migration related code. If you started using migrations with existing database you first have to revert all migrations by using the second or third command, then delete `MigrationHistory` table and all migration related code. It should get you to starting position. You can also get starting database from backup (before using migrations) and delete all migration related code. – Ladislav Mrnka Dec 30 '12 at 17:25
  • 4
    @MichaelBlackburn: what I meant was that all the other solutions I'd seen tell you to update back to your first **named** migration, but my problem was that there wasn't an initial _clean_ migration to update (back) to -- I just wanted to get rid of all migrations, regardless of the method. Therefore this answer addressed my concern with `-TargetMigration:0` – drzaus Jan 13 '13 at 17:38
28

For Entity Framework Core:

Update-Database -Migration:0
Remove-Migration
Andrei Karcheuski
  • 3,116
  • 3
  • 38
  • 39
8

To be clear, if using LocalDb, when you want to start from scratch just delete the database via the Database Explorer and then type enable-migrations -force in the Package Manager Console. Do not delete the database via the App_Data folder or you will have the following issue.

Community
  • 1
  • 1
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • 2
    Be careful when doing this as it will overwrite your Migrations\Configuration.cs without warning! – cgatian Jul 01 '14 at 23:36
  • Thanks for the warning, I kind of pretty much dropped the code first way after a while, its nice to have all your code in once, place, have source control versioning for your database per se but I didn't see any other big benefit and switched back to database first. I use Red Gate Sql Compare to track all deltas. – Brian Ogden Jul 02 '14 at 02:30
5
Update-Database -Migration 0
Remove-Migration

The documentation is here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#update-database and here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#remove-migration

Sergey
  • 1,020
  • 11
  • 22
0

It is written wrong in their documentation i guess , for me i used

Update-Database -Target MigrationName
Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25