实体框架迁移重命名表和列 [英] Entity Framework Migrations renaming tables and columns

查看:338
本文介绍了实体框架迁移重命名表和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改名了几个实体及其导航性能和产生的EF 5.新的迁移正如往常一样在EF迁移重命名,默认情况下它是要删除对象并重新创建它们。这不是我想要的东西,所以我几乎不得不从头开始构建迁移文件。

I renamed a a couple entities and their navigation properties and generated a new Migration in EF 5. As is usual with renames in EF migrations, by default it was going to drop objects and recreate them. That isn't what I wanted so I pretty much had to build the migration file from scratch.

    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 。然后,我需要从分组ID dbo.ReportPages 外键列重命名为 SECTION_ID

All I'm trying to do is rename dbo.ReportSections to dbo.ReportPages and then dbo.ReportSectionGroups to dbo.ReportSections. Then I need to rename the foreign key column on dbo.ReportPages from Group_Id to Section_Id.

我放弃了外键和表连接在一起索引,那么我重命名表和外键列,那么我M重新添加索引和外键。我以为这是去上班,但我得到一个SQL错误。

I am dropping the foreign keys and indexes linking the tables together, then I am renaming the tables and the foreign key column, then I'm adding the indexes and foreign keys again. I assumed this was going to work but I am getting a SQL error.

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

Msg 15248, Level 11, State 1, Procedure sp_rename, Line 215 Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong. Msg 4902, Level 16, State 1, Line 10 Cannot find the object "dbo.ReportSections" because it does not exist or you do not have permissions.

我并没有一个简单的时间搞清楚什么是错在这里。任何有识之士将极大的帮助。

I'm not having an easy time figuring out what is wrong here. Any insight would be tremendously helpful.

推荐答案

没关系。我正在这种方法比它更复杂真的需要如此。

Nevermind. I was making this way more complicated than it really needed to be.

这是所有我需要的。重命名方法只生成到[sp_rename来]系统存储过程的调用,我猜了所有事情,包括新的列名外键。

This was all that I needed. The rename methods just generate a call to the [sp_rename] system stored procedure and I guess that took care of everything, including the foreign keys with the new column name.

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");
}

这篇关于实体框架迁移重命名表和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆