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).
By [InverseProperty("Foo")]
you tell EF that Bar.Foo
and Foo.Bars
are paired properties in a one to many association, so that's clear.
Then there are Foo.MainBar
and Bar.OldFoo
. EF does not know how these are related. They could be paired in a one-to-one association, they can be independent, i.e. with a "many" multiplicity on the other side. So you have to tell EF.
I assume that the properties are independent, i.e. that a Bar
can have an OldFoo
without the requirement that this Bar
is Foo
's MainBar
at the same time. Then it is enough to give EF information about one of the properties:
modelBuilder.Entity<Bar>().HasOptional(f => f.OldFoo).WithMany()
.HasForeignKey(f => f.OldFooId);
or
modelBuilder.Entity<Foo>().HasOptional(f => f.MainBar)
.WithRequired(b => b.OldFoo)
As there are no inverse properties paired with these "one" ends of the associations you can't do this with data annotations (there are no properties to adorn with attributes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With