简体   繁体   English

EF 4.3迁移重命名列而不是删除它

[英]EF 4.3 migration renaming column instead of deleting it

I have this class: 我有这门课:

public class HabitDo{
    public int? HabitId { get; set; }
    virtual public Habit Habit { get; set; }

    public int DoId { get; set; }
    virtual public Do Do { get; set; }

    public string Restriction { get; set; }

    public int? ObjectiveId { get; set; }
    public Objective Objective { get; set; }

    public virtual ICollection<DoObjective> Objectives { get; set; }
}

The table is just fine, but then I remove from code the Objective property: 该表很好,但后来我从代码中删除了Objective属性:

 public class HabitDo{
    public int? HabitId { get; set; }
    virtual public Habit Habit { get; set; }

    public int DoId { get; set; }
    virtual public Do Do { get; set; }

    public string Restriction { get; set; }

    public virtual ICollection<DoObjective> Objectives { get; set; }
}

And when calling update-database from the manager console EF renames the ObjectiveId column instead of dropping it: 从管理器控制台EF调用update-database时,重命名ObjectiveId列而不是删除它:

    EXECUTE sp_rename @objname = N'HabitDoes.ObjectiveId', @newname =  N'Objective_Id', @objtype = N'COLUMN'

Any clues why this happens? 任何线索为什么会发生这种情况?

It is because you probably still have the existing one-to-many relationship - you've just removed navigation property on one side of the relation but the other side still exists. 这是因为您可能仍然存在现有的一对多关系 - 您刚刚删除了关系一侧的导航属性,但另一侧仍然存在。 Because of that EF must keep FK column in your table. 因为EF必须在表格中保留FK列。 EF just renames the hidden FK column to default naming convention. EF只是将隐藏的FK列重命名为默认命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM