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 associ
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).