Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

 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?

like image 987
joaoruimartins Avatar asked Jul 25 '12 09:07

joaoruimartins


People also ask

How do I reset my EF migration?

Resetting all migrations This can be easily done by deleting your Migrations folder and dropping your database; at that point you can create a new initial migration, which will contain your entire current schema.


1 Answers

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.

like image 99
Ladislav Mrnka Avatar answered Sep 28 '22 10:09

Ladislav Mrnka