且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法确定关联的主要目的

更新时间:2023-11-12 23:23:22

通过 [InverseProperty( Foo)] ,您告诉EF Bar.Foo Foo.Bars 是一对多关联的配对属性,所以很清楚。

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.

然后在那里是 Foo.MainBar Bar.OldFoo 。 EF不知道它们之间的关系。它们可以一对一地配对,它们可以是独立的,即在另一侧具有许多多重性。因此,您必须告诉EF。

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.

我假设属性是独立的,即 Bar 可以具有 OldFoo ,而不要求此 Bar Foo MainBar 同时。这样就足以为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);

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