Я переименовал пару объектов и их навигационные свойства и создал новую миграцию в 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.
Msg 15248, уровень 11, состояние 1, процедура sp_rename, строка 215 Либо параметр @objname неоднозначен, либо заявленный код @objtype (COLUMN) ошибочен. Msg 4902, уровень 16, состояние 1, строка 10 Не удается найти объект "dbo.ReportSections", потому что он не существует или у вас нет разрешений.
Мне нелегко выяснить, что здесь не так. Любое понимание было бы чрезвычайно полезным.