90

I have created a migration using the Add-Migration command, but I'd like to change the name of that migration. How can I undo the migration command, so that I can regenerate it using the new desired name?

Is it just a matter of deleting the generated files, or this could be a bad idea?

Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82

7 Answers7

158

If you haven't used Update-Database you can just delete the migration file. If you've run the update you should roll it back using Update-Database -TargetMigration "NameOfPreviousMigration" then delete the migration file.

Reference:

http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/

Colin
  • 22,328
  • 17
  • 103
  • 197
17

If you haven't executed the migration yet with Update-Database, you can run Add-Migration again with the same name (you may need to use -Force) to re-execute the scaffolding. This is noted in the output of the Add-Migration command.

Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25
Adrian
  • 42,911
  • 6
  • 107
  • 99
14

Just use command

Remove-migration

It will remove last added migration and update snapshot. It will not affect database so you have to rollback db in first place.

Tomasz Maj
  • 1,541
  • 15
  • 15
6

To add to @Ben 's answer, when using the dotnet ef command variety this is the remove command you need:

dotnet ef migrations remove

Which will remove your last migration and update the model snapshot.

veuncent
  • 1,599
  • 1
  • 20
  • 17
4

With EntityFrameworkCore 2.0 comes the model snapshot. You will need to run the remove migration command in order to update the model snapshot. I have read that EF Core will recognize any update and revert the snapshot for you if you manually delete the migration but this has not worked for me.

https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-remove

Ben
  • 1,820
  • 2
  • 14
  • 25
3

Update your last perfect migration via this command :

Update-Database –TargetMigration
Abdus Salam Azad
  • 5,087
  • 46
  • 35
0

Just use command:

Update-Database nameMigration -context nameContext

And Then

Remove-migration -Context nameContext