You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I then ran Update-Database. But this still leaves a bunch of indexes and foreign keys that should also be renamed. So I used the Add-Migration command to bring any remaining changes up to date.
But I notice the new migration seems to be dropping and recreating tables and columns that my custom migration already renamed.
My question is: What is the Add-Migration command comparing? Is it looking at the database or something else? Why didn't it recognize that these tables and columns have been renamed?
How can I get it to recognize the changes in my custom migration?
@SoftCircuits It's building the EF model and comparing to the previous EF model contained in the Migrations snapshot. If you are sure that your database matches the model, then you can delete these drops and apply the migration. This will tell EF that the database and model are in sync.
However, having said all that, I would like to understand why you say, "Since I don't want to lose the existing data, I created a migration from scratch." The migration EF scaffolds to do this should not cause data loss.
@ajcvickers Thanks. Where can I learn more about this snapshot or if there's any way to apply a custom schema to it?
As far as losing data, if it drops the old table and recreates a new one with the new name, I don't see how the data could not be lost. Moreover, if I simply rename one of my entity classes, I don't think EF even knows it was renamed and not a new table. But if there's an easier way to do this, I'm all ears!
@SoftCircuits Migration in EF Core 3.1 and below never drops and recreates the table. In EF Core 5.0, we will do this in some situations when targeting SQLite, but this is done in a way that preserves the existing data. The Migrations documentation is the best place to learn more.
@ajcvickers Thanks for the link. I'll check it out. But the second snippet I posted seems to show it is dropping tables. So I'm not sure if DropTable means something other than what I think it means.
If it's doing that without your custom migration being there then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
@ajcvickers I'll be respectful of your time but can I ask just one more question? If I eliminate my custom migration, are you saying I can rename a table and column just by renaming the entity class and property? Does it have some way to know that the newly named class is renamed and not a new class and column?
@ajcvickers It drops and recreates the two tables. Unfortunately, if I edit the migration, there doesn't appear to be any way to apply the edits to the snapshot.
Moved from discussion #21754.
Question
From @SoftCircuits
I'm renaming several tables and columns. Since I don't want to lose the existing data, I created a migration from scratch.
I then ran
Update-Database
. But this still leaves a bunch of indexes and foreign keys that should also be renamed. So I used theAdd-Migration
command to bring any remaining changes up to date.But I notice the new migration seems to be dropping and recreating tables and columns that my custom migration already renamed.
My question is: What is the
Add-Migration
command comparing? Is it looking at the database or something else? Why didn't it recognize that these tables and columns have been renamed?How can I get it to recognize the changes in my custom migration?
Replies
From @ajcvickers
@SoftCircuits It's building the EF model and comparing to the previous EF model contained in the Migrations snapshot. If you are sure that your database matches the model, then you can delete these drops and apply the migration. This will tell EF that the database and model are in sync.
However, having said all that, I would like to understand why you say, "Since I don't want to lose the existing data, I created a migration from scratch." The migration EF scaffolds to do this should not cause data loss.
From @SoftCircuits
@ajcvickers Thanks. Where can I learn more about this snapshot or if there's any way to apply a custom schema to it?
As far as losing data, if it drops the old table and recreates a new one with the new name, I don't see how the data could not be lost. Moreover, if I simply rename one of my entity classes, I don't think EF even knows it was renamed and not a new table. But if there's an easier way to do this, I'm all ears!
From @ajcvickers
@SoftCircuits Migration in EF Core 3.1 and below never drops and recreates the table. In EF Core 5.0, we will do this in some situations when targeting SQLite, but this is done in a way that preserves the existing data. The Migrations documentation is the best place to learn more.
From @SoftCircuits
@ajcvickers Thanks for the link. I'll check it out. But the second snippet I posted seems to show it is dropping tables. So I'm not sure if DropTable means something other than what I think it means.
From @ajcvickers
If it's doing that without your custom migration being there then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
From @SoftCircuits
@ajcvickers I'll be respectful of your time but can I ask just one more question? If I eliminate my custom migration, are you saying I can rename a table and column just by renaming the entity class and property? Does it have some way to know that the newly named class is renamed and not a new class and column?
From @ajcvickers
It will try based on the amount of similarity.
From @SoftCircuits
@ajcvickers It drops and recreates the two tables. Unfortunately, if I edit the migration, there doesn't appear to be any way to apply the edits to the snapshot.
From @ajcvickers
@SoftCircuits Discussed with the team and apparently the renaming logic is weak currently--making it better is tracked by #5826
The text was updated successfully, but these errors were encountered: