【发布时间】:2020-11-13 09:11:38
【问题描述】:
我正在重命名几个表和列。由于我不想丢失现有数据,因此我从头开始创建了迁移。
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");
}
然后我跑了Update-Database。但这仍然留下了一堆也应该重命名的索引和外键。所以我使用了Add-Migration 命令来更新任何剩余的更改。
但我注意到新迁移似乎正在删除并重新创建我的自定义迁移已重命名的表和列。
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");
我的问题是:Add-Migration 命令比较的是什么?它是在查看数据库还是其他什么?为什么它没有识别出这些表和列已被重命名?
如何让它识别我的自定义迁移中的更改?
【问题讨论】:
标签: c# .net entity-framework entity-framework-core code-first