Renaming entity will drop and create the table instead of renaming it only.
This is an inherent limitation of EF, not Pomelo directly. EF cannot normally distinguish between a table drop/create, and a rename.
The trick to tell it that it's a rename is to set the table name manually, either via an attribute on the entity class, or via the OnModelCreating fluent API.
The sequence goes like:
E.g builder.Entity<OriginalEntity>().ToTable("RenamedEntity");
Generate a migration. This will create the correct rename table migration for the entity.
Verify this migration is correct. If you are using Pomelo 2.2.6, check for migrationBuilder.DropPrimaryKey
and AddPrimaryKey
entries and delete them, since these are not necessary for MySQL and will cause issues with generating idempotent SQL scripts. This is fixed in 3.1.
Rename the actual entity class.
Generate another migration to update the model snapshot with the new entity names.
If you then want to remove the .ToTable("RenamedEntity");
to fall back to convention based table naming:
Remove the .ToTable("RenamedEntity");
. If you got the table name correct, the convention based name should be the same as the current name.
Generate a final migration to verify that the table isn't renamed again. If it was, revert all steps, correct the name, and redo. If the migration was empty, you can revert it.
I'm yet to test it out, but you can probably do this in the reverse order to. I.E:
.ToTable
with the current table name.ToTable
attributeFor more information, see this stack overflow question:
https://stackoverflow.com/questions/13296996/entity-framework-migrations-renaming-tables-and-columns
Basically, there are heuristics implemented, that can detect a table rename in _some_ cases.
But renaming is one of those operations, where you definitely want to check the generated migration code afterwards and change the DropTable
and CreateTable
calls to RenameTable
where necessary.
EF Core has rename table just like these auto generated migration.
migrationBuilder.DropPrimaryKey(
name: "PK_LogFieldsConfiguration",
table: "LogFieldsConfiguration");
migrationBuilder.RenameTable(
name: "LogFieldsConfiguration",
newName: "LogFieldsConfigurations");
migrationBuilder.AddPrimaryKey(
name: "PK_LogFieldsConfigurations",
table: "LogFieldsConfigurations",
column: "Id");
Most helpful comment
This is an inherent limitation of EF, not Pomelo directly. EF cannot normally distinguish between a table drop/create, and a rename.
The trick to tell it that it's a rename is to set the table name manually, either via an attribute on the entity class, or via the OnModelCreating fluent API.
The sequence goes like:
E.g
builder.Entity<OriginalEntity>().ToTable("RenamedEntity");
Generate a migration. This will create the correct rename table migration for the entity.
Verify this migration is correct. If you are using Pomelo 2.2.6, check for
migrationBuilder.DropPrimaryKey
andAddPrimaryKey
entries and delete them, since these are not necessary for MySQL and will cause issues with generating idempotent SQL scripts. This is fixed in 3.1.Rename the actual entity class.
Generate another migration to update the model snapshot with the new entity names.
If you then want to remove the
.ToTable("RenamedEntity");
to fall back to convention based table naming:Remove the
.ToTable("RenamedEntity");
. If you got the table name correct, the convention based name should be the same as the current name.Generate a final migration to verify that the table isn't renamed again. If it was, revert all steps, correct the name, and redo. If the migration was empty, you can revert it.
I'm yet to test it out, but you can probably do this in the reverse order to. I.E:
.ToTable
with the current table name.ToTable
attributeFor more information, see this stack overflow question:
https://stackoverflow.com/questions/13296996/entity-framework-migrations-renaming-tables-and-columns