Unable to determine the principal end of an association

后端 未结 1 1918
忘掉有多难
忘掉有多难 2021-01-13 16:57

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

相关标签:
1条回答
  • 2021-01-13 17:50

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

    0 讨论(0)
提交回复
热议问题