Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename a migration in entity framework core

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?

like image 217
mynameisjeff Avatar asked May 26 '17 11:05

mynameisjeff


People also ask

What is Entity Framework Core migration?

Migration in Entity Framework Core Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.

How to migrate from one property to another in EF Core?

If you want to use this method for EF Core, definitely do the two steps with two separate migrations: ColumnChanged_A, then ColumnChanged_B. If you don't you risk having your DBModelSnapshot reference the old Property name. (maybe this will always sort itself in later migrations, but still seems kinda risky)

What happens when you rename a property in EF Core?

For example, if you rename a property from Name to FullName, EF Core will generate the following migration: EF Core is generally unable to know when the intention is to drop a column and create a new one (two separate changes), and when a column should be renamed. If the above migration is applied as-is, all your customer names will be lost.

How do I remove a migration from my EF Core Model?

Sometimes you add a migration and realize you need to make additional changes to your EF Core model before applying it. To remove the last migration, use this command. Remove-Migration dotnet ef migrations remove. After removing the migration, you can make the additional model changes and add it again.


2 Answers

If it's just the class name conflicting, only renaming the class should be sufficient.

If you really want to rename the whole migration, your steps look correct. Make sure you check the *.Designer.cs file to rename the string literal containing the name.

like image 150
bricelam Avatar answered Oct 13 '22 01:10

bricelam


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.

like image 2
JakeMc Avatar answered Oct 13 '22 01:10

JakeMc