我重命名了一對夫婦實體及其導航屬性,並在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嗎? –