我重命名了一对夫妇实体及其导航属性,并在EF 5中生成了新的迁移。与EF迁移中的重命名一样,默认情况下,它将删除对象并重新创建它们。这不是我想要的,所以我几乎必须从头开始构建迁移文件。实体框架迁移重命名表和列
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
。然后我需要将dbo.ReportPages
的外键列从Group_Id
重命名为Section_Id
。
我在删除将表连接在一起的外键和索引,然后我重命名表和外键列,然后再次添加索引和外键。我认为这是工作,但我得到一个SQL错误。
消息15248,级别11,状态1,过程sp_rename可以,215线 要么参数@objname不明确或所要求保护的@objtype(列)是错误的。 Msg 4902,Level 16,State 1,Line 10 找不到对象“dbo.ReportSections”,因为它不存在或您没有权限。
我没有一个容易的时间搞清楚这里有什么问题。任何见解都将非常有帮助。
了上述线路的失败?你能跟踪SQL Server Profiler中的迁移并检查相应的SQL吗? –