且构网

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

实体框架(Database-First)多个关系到同一表命名约定控件

更新时间:2023-09-13 10:02:34

从条目使用反向导航属性编程实体框架:代码优先


你可以添加配置(使用数据注释或Fluent API)
将此信息提供给模型构建器。使用Data
注释,您将使用一个名为 InverseProperty 的注释。使用
Fluent API,您将使用Has / With方法的组合来
指定这些关系的正确结尾。

You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.

您可以将注释放在关系的两端(如果需要,则
都会结束)。我们将把它们放在Lodging类的导航属性
(示例4-10)中。 InverseProperty Data
注释需要
中相应导航属性的名称作为参数。

You can place the annotations on either end of the relationship (or both ends if you want). We’ll stick them on the navigation properties in the Lodging class (Example 4-10). The InverseProperty Data Annotation needs the name of the corresponding navigation property in the related class as its parameter.

示例:

[InverseProperty("PrimaryContactFor")]
public Person PrimaryContact { get; set; }

[InverseProperty("SecondaryContactFor")]
public Person SecondaryContact { get; set; }