实体框架代码首次迁移-删除列

问题描述 投票:0回答:2

[当需要使用Entity Framework Code First Migrations向表中添加新列时,您只需执行以下操作:“ add-migration newColumn”,然后“ update-database”。删除newColumn的具体命令是什么?

“ remove-migration newColumn”,然后是“ update-database”?

asp.net entity-framework migration code-first ef-migrations
2个回答
2
投票

您的问题非常令人困惑,您是否要还原迁移,还是只是删除先前添加的列?

简单的答案,修改模型(通过删除列)并添加新的迁移。此新迁移将包括DropColumn命令以删除列。

如果您尝试还原迁移,则需要:

update-database -TargetMigration: "Migration" 

其中“迁移”是您要还原的迁移之前的迁移名称。此外,如果您需要还原到原始状态,可以使用:

update-database -TargetMigration: $InitialDatabase 

MSDN文章对此进行了很好的解释:https://msdn.microsoft.com/en-us/data/jj591621.aspx


0
投票

我已经使用实体框架代码优先方法使用“ Add-Migration AddMobileNo”在表中成功添加了新列。

[Table("Student")]
public class Student
{
    [Key]
    public int StudentId { get; set; }
    public string Name { get; set; }
    // Add new column using "Add-Migration AddMobileNo"
    public string MobileNo { get; set; } 
}

public partial class AddMobileNo : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Student", "MobileNo", c => c.String());
        }

        public override void Down()
        {
            DropColumn("dbo.Student", "MobileNo");
        }
    }

现在我正在尝试使用以下命令调用down()方法> 还原迁移

update-database -TargetMigration:“ AddMobileNo”

但是它对我不起作用,请在PMC(软件包管理器控制台)中提供以下消息。

目标数据库已经是AddMobileNo版本

提前感谢。

© www.soinside.com 2019 - 2024. All rights reserved.