多个关系到同一表命名约定控件

多个关系到同一表命名约定控件

本文介绍了实体框架(Database-First)多个关系到同一表命名约定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我们有这样的情况:

Let's suppose that we have this situation:

数据库中的表:

Country(id,country_name),Person(id,login),CountryManager(id_country,id_person),CountryStakeholder(id_country,id_person)

如果我们必须从数据库创建模型,使用Entity Framework Database-First,在VS中,我们将有一个类,如

If we had to create the model from the database, using Entity Framework Database-First, in VS we'd have a class like this:

class Country {

int id;
string country_name;

virtual ICollection<Person> Person1; // Navigation Properties
virtual ICollection<Person> Person2; // ---------||----------

}

我已经简化了代码,但希望得到点。

I've simplified the code a lot, but hopefully you got the point.

看起来,当实体框架处理外部键创建通用导航属性。是否有可能控制如何通过名称创建导航属性? Person1,Person2不是很解释,不幸的是。

Seems that when Entity Framework deals with foreign keys it creates generic Navigation Properties. Is there a possibility to control how Navigation Properties are created by name? Person1, Person2 isn't very explainatory, unfortunately.

推荐答案

从条目从:

您可以将注释放在关系的两端(如果需要,则
都会结束)。我们将把它们放在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; }

这篇关于实体框架(Database-First)多个关系到同一表命名约定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:38