entity framework迁移重命名表和列

我重命名了一对夫妇实体及其导航属性,并在EF 5中生成了新的迁移。与EF迁移中的重命名一样,默认情况下,它将删除对象并重新创build它们。 这不是我想要的,所以我几乎不得不从头构build迁移文件。

public override void Up() { DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports"); DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups"); DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections"); DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" }); DropIndex("dbo.ReportSections", new[] { "Group_Id" }); DropIndex("dbo.Editables", new[] { "Section_Id" }); RenameTable("dbo.ReportSections", "dbo.ReportPages"); RenameTable("dbo.ReportSectionGroups", "dbo.ReportSections"); RenameColumn("dbo.ReportPages", "Group_Id", "Section_Id"); AddForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports", "Id"); AddForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections", "Id"); AddForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages", "Id"); CreateIndex("dbo.ReportSections", "Report_Id"); CreateIndex("dbo.ReportPages", "Section_Id"); CreateIndex("dbo.Editables", "Page_Id"); } public override void Down() { DropIndex("dbo.Editables", "Page_Id"); DropIndex("dbo.ReportPages", "Section_Id"); DropIndex("dbo.ReportSections", "Report_Id"); DropForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages"); DropForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections"); DropForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports"); RenameColumn("dbo.ReportPages", "Section_Id", "Group_Id"); RenameTable("dbo.ReportSections", "dbo.ReportSectionGroups"); RenameTable("dbo.ReportPages", "dbo.ReportSections"); CreateIndex("dbo.Editables", "Section_Id"); CreateIndex("dbo.ReportSections", "Group_Id"); CreateIndex("dbo.ReportSectionGroups", "Report_Id"); AddForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections", "Id"); AddForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups", "Id"); AddForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports", "Id"); } 

我想要做的是将dbo.ReportSections重命名为dbo.ReportPages ,然后dbo.ReportSectionGroups重命名为dbo.ReportSections 。 然后,我需要将Group_IdGroup_Id的外键列重命名为Section_Id

我删除外键和索引链接表在一起,然后我重命名表和外键列,然后我再次添加索引和外键。 我认为这是工作,但我得到一个SQL错误。

消息15248,级别11,状态1,过程sp_rename,行215参数@objname不明确或声明的@objtype(COLUMN)错误。 消息4902,级别16,状态1,行10找不到对象“dbo.ReportSections”,因为它不存在或您没有权限。

我不是很容易找出这里有什么问题。 任何见解都将非常有帮助。

没关系。 我正在使这种方式比真正需要的更复杂。

这就是我所需要的。 重命名方法只是生成一个调用[sp_rename]系统存储过程,我想这一切照顾,包括新的列名称的外键。

 public override void Up() { RenameTable("ReportSections", "ReportPages"); RenameTable("ReportSectionGroups", "ReportSections"); RenameColumn("ReportPages", "Group_Id", "Section_Id"); } public override void Down() { RenameColumn("ReportPages", "Section_Id", "Group_Id"); RenameTable("ReportSections", "ReportSectionGroups"); RenameTable("ReportPages", "ReportSections"); } 

如果您不喜欢手动编写/更改Migration类中所需的代码,则可以按照两步方法自动创build所需的RenameColumn代码:

第一步使用ColumnAttribute引入新的列名,然后添加迁移(例如Add-Migration ColumnChanged

 public class ReportPages { [Column("Section_Id")] //Section_Id public int Group_Id{get;set} } 

第二步更改属性名称,并再次应用于包pipe理器控制台中的相同迁移(例如Add-Migration ColumnChanged

 public class ReportPages { [Column("Section_Id")] //Section_Id public int Section_Id{get;set} } 

如果你看看Migration类,你可以看到自动生成的代码是RenameColumn

我只是在EF6(代码第一个实体重命名)尝试相同。 我只是重命名类,并使用包pipe理器控制台添加了迁移,瞧,使用RenameTable(…)的迁移自动为我生成。 我不得不承认,对实体的唯一更改是重命名它,所以没有新的列或重命名的列,所以我不能确定,如果这是一个EF6的东西,或者只是EF(总是)能够检测到这样简单的迁移。

要扩大一点Hossein Narimani Rad的答案,你可以使用System.ComponentModel.DataAnnotations.Schema.TableAttribute和System.ComponentModel.DataAnnotations.Schema.ColumnAttribute分别重命名表和列。

这有几个好处:

  1. 这不仅会自动创build名称迁移,而且会自动创build名称迁移
  2. 它也将美味地删除任何外键,并重新创build新的表和列名称,给外键和合适的名称。
  3. 所有这些都不会丢失任何表格数据

例如,添加[Table("Staffs")]

 [Table("Staffs")] public class AccountUser { public long Id { get; set; } public long AccountId { get; set; } public string ApplicationUserId { get; set; } public virtual Account Account { get; set; } public virtual ApplicationUser User { get; set; } } 

将生成迁移:

  protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_AccountUsers_Accounts_AccountId", table: "AccountUsers"); migrationBuilder.DropForeignKey( name: "FK_AccountUsers_AspNetUsers_ApplicationUserId", table: "AccountUsers"); migrationBuilder.DropPrimaryKey( name: "PK_AccountUsers", table: "AccountUsers"); migrationBuilder.RenameTable( name: "AccountUsers", newName: "Staffs"); migrationBuilder.RenameIndex( name: "IX_AccountUsers_ApplicationUserId", table: "Staffs", newName: "IX_Staffs_ApplicationUserId"); migrationBuilder.RenameIndex( name: "IX_AccountUsers_AccountId", table: "Staffs", newName: "IX_Staffs_AccountId"); migrationBuilder.AddPrimaryKey( name: "PK_Staffs", table: "Staffs", column: "Id"); migrationBuilder.AddForeignKey( name: "FK_Staffs_Accounts_AccountId", table: "Staffs", column: "AccountId", principalTable: "Accounts", principalColumn: "Id", onDelete: ReferentialAction.Cascade); migrationBuilder.AddForeignKey( name: "FK_Staffs_AspNetUsers_ApplicationUserId", table: "Staffs", column: "ApplicationUserId", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Restrict); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_Staffs_Accounts_AccountId", table: "Staffs"); migrationBuilder.DropForeignKey( name: "FK_Staffs_AspNetUsers_ApplicationUserId", table: "Staffs"); migrationBuilder.DropPrimaryKey( name: "PK_Staffs", table: "Staffs"); migrationBuilder.RenameTable( name: "Staffs", newName: "AccountUsers"); migrationBuilder.RenameIndex( name: "IX_Staffs_ApplicationUserId", table: "AccountUsers", newName: "IX_AccountUsers_ApplicationUserId"); migrationBuilder.RenameIndex( name: "IX_Staffs_AccountId", table: "AccountUsers", newName: "IX_AccountUsers_AccountId"); migrationBuilder.AddPrimaryKey( name: "PK_AccountUsers", table: "AccountUsers", column: "Id"); migrationBuilder.AddForeignKey( name: "FK_AccountUsers_Accounts_AccountId", table: "AccountUsers", column: "AccountId", principalTable: "Accounts", principalColumn: "Id", onDelete: ReferentialAction.Cascade); migrationBuilder.AddForeignKey( name: "FK_AccountUsers_AspNetUsers_ApplicationUserId", table: "AccountUsers", column: "ApplicationUserId", principalTable: "AspNetUsers", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }