实体框架(Database-First)多个关系到同一表命名约定控件 [英] Entity Framework (Database-First) multiple relations to same table naming conventions control

本文介绍了实体框架(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.

推荐答案

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


你可以添加配置(使用数据注释或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; }

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆