ChatGPT解决这个技术问题 Extra ChatGPT

Entity Framework - Start Over - Undo/Rollback All Migrations

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.


K
Kyle Trauberman

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.


as usual, you're the best.
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).
@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.
@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
A
Andrei Karcheuski

For Entity Framework Core:

Update-Database -Migration:0
Remove-Migration

C
Community

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.


Be careful when doing this as it will overwrite your Migrations\Configuration.cs without warning!
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.
S
Sayed Muhammad Idrees

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

Update-Database -Target MigrationName