Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does Add-Migration command use to compare schemas? #22775

Closed
ajcvickers opened this issue Sep 25, 2020 · 0 comments
Closed

What does Add-Migration command use to compare schemas? #22775

ajcvickers opened this issue Sep 25, 2020 · 0 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ajcvickers
Copy link
Member

ajcvickers commented Sep 25, 2020

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.

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.RenameTable(name: "Providers", schema: "dbo", newName: "Vendors", newSchema: "dbo");
    migrationBuilder.RenameTable(name: "ProviderContacts", schema: "dbo", newName: "VendorContacts", newSchema: "dbo");
    migrationBuilder.RenameColumn(name: "ProviderId", table: "AspNetUsers", newName: "VendorId", schema: "dbo");
    migrationBuilder.RenameColumn(name: "ProviderId", table: "VendorContacts", newName: "VendorId", schema: "dbo");
    migrationBuilder.RenameColumn(name: "ProviderId", table: "Locations", newName: "VendorId", schema: "dbo");
}

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.

migrationBuilder.DropTable(
    name: "ProviderContacts");

migrationBuilder.DropTable(
    name: "Providers");

migrationBuilder.DropIndex(
    name: "IX_Locations_ProviderId",
    table: "Locations");

migrationBuilder.DropIndex(
    name: "IX_AspNetUsers_ProviderId",
    table: "AspNetUsers");

migrationBuilder.DropColumn(
    name: "ProviderId",
    table: "Locations");

migrationBuilder.DropColumn(
    name: "ProviderId",
    table: "AspNetUsers");

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

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Sep 25, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

1 participant