Unable to determine the principal end of an association

社会主义新天地 提交于 2019-12-01 06:53:30

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!