Entity Framework Core Interview Question #97
What are the risks of renaming a column or table through an EF Core migration?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
A model rename may sometimes be interpreted as removing the old database object and creating a new one. If a generated migration drops a column and adds another, existing data can be lost.
Detailed Explanation
A model rename may sometimes be interpreted as removing the old database object and creating a new one. If a generated migration drops a column and adds another, existing data can be lost.
When the intention is a rename, review the migration and use explicit RenameColumn or RenameTable operations where appropriate.
This is a strong example of why generated migrations must be inspected rather than accepted automatically.
Code Example
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "OldName",
table: "Products",
newName: "Name");
}