I have the following classes in my model – but I cant get it to create tables the way I expect.
public class Task
{
public int Id { get; set; }
public string Description { get; set; }
private readonly BindingList<Dependancy> _dependancies = new BindingList<Dependancy>();
public virtual BindingList<Dependancy> Dependancies
{
get { return _dependancies; }
}
}
public class Dependancy
{
public int Id { get; set; }
[Required]
public virtual Task Task { get; set; }
[Required]
public virtual Task NeededTask { get; set; }
}
I want the Dependancy table just to contain the following fields;
Id,
Task_Id,
NeededTask_Id
however the generated one contains an extra Task_Id1 field
I have tried the following overide in the context class;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Dependancy>().HasRequired( p=>p.Task ).WithMany().WillCascadeOnDelete(false);
modelBuilder.Entity<Task>().HasMany<Dependancy>(c => c.Dependancies).WithOptional().WillCascadeOnDelete(false);
}
but the unwanted field is still created.
Entity framework does not know that
Dependancy.TaskandTask.Dependanciesare paired, so it creates two foreign key fields, one for both associations. You have to tell EF that both associations are counterparts:The other association can be defined by