In this kind of association I get the error "Unable to determine the principal end of an association between the types 'Foo' and 'Bar'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
public class Foo
{
public int Id { get; set; }
public int? MainBarId { get; set; }
public virtual Bar MainBar { get; set; }
[InverseProperty("Foo")]
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
public int FooId { get; set; }
public virtual Foo Foo { get; set; }
public int? OldFooId { get; set; }
public virtual Foo OldFoo { get; set; }
}
Here the Foo has a collection of Bars and could have a main Bar (MainBar). The Bar always was associated with a Foo but could have a reference to another Foo (OldFoo).
- How to map this in EF with Data Annotations?
- If not possible with Data Annotations, how to do this with fluent?