how to rename a migration in entity framework core

Viewed 5161

How could I change the name of an old migration (rename it)? It conflicts with the name of my custom identity user.

Would changing the identity user be easier? I have no idea how to rename that either, every tutorial out there just create a new db for the sake of simplicity (any information about that is appreciated too).

Is it as simple as renaming it in:

  • it's migration .cs file
  • the db migration table
  • the snapshot

Is that it? Any idea if this could screw things up?

2 Answers

As far as a direct answer to the actual question since I got here by googling:

It's not a simple as just renaming the migration file. Especially if the migration has already gone out to client databases.

I've found that EF still remembers the name of the old migration somewhere and ends up still inserting the old name in the __EFMigrationsHistory table even after renaming the migration file, designer file, and class name.

For me it was easier to copy all the code in the Up and Down methods to a notepad, use remove-migration to delete the wrongly named migration, then use add-migration to add a new one with the desired name and copy back in all the code from the notepad I saved. However, that ONLY works if the migration is the latest one.

If it's an older one, already out to client dbs, or superseded by other migrations, you'll have to worry about updating the __EFMigrationsHistory table manually somehow and EF's memory of the migration name wherever that is, which could get pretty tricky.

Related