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:
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:
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 just renames the hidden FK column to default naming convention.