EF 4.3迁移重命名列,而不是将其删除

问题描述:

我有这个类:EF 4.3迁移重命名列,而不是将其删除

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

表就好了,但后来我从代码中删除目标属性:

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

并调用时更新数据库从管理器控制台EF重命名ObjectiveId列而不是丢弃它:

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

任何线索为什么会发生?

这是因为您可能仍然存在现有的一对多关系 - 您刚删除关系一侧的导航属性,但另一侧仍然存在。因为EF必须在您的表格中保留FK列。 EF只是将隐藏的FK列重命名为默认命名约定。

+1

就是这样!谢谢 – joaoruimartins 2012-07-25 10:54:01