简体   繁体   English

获取简单属性和导航属性之间的映射-Entity Framework 5

[英]Get mapping between simple property and navigation property - Entity Framework 5

I have an entity with below properties: 我有一个具有以下属性的实体:

Simple Properties: 简单属性:

public int ParentLocationID {get; set;}
public int ChildLocationID {get; set;}

Navigation Properties: 导航属性:

public Location Location1 {get; set;}
public Location Location2 {get; set;}

Here, both the simple properties refer to the foreign key relationship with a Location Table. 这里,这两个简单属性都引用了与位置表的外键关系。 This information is available when I open the Designer.cs for the model. 当我打开模型的Designer.cs时,此信息可用。

But how can I know in the code that ParentLocationID is using which navigation property Location1 or Location2? 但是我如何在代码中知道ParentLocationID正在使用哪个导航属性Location1或Location2?

Use the ForeignKey annotation? 使用ForeignKey注释?

using System.ComponentModel.DataAnnotations.Schema;

public class MyModel
{
    public int ParentLocationID {get; set;}
    public int ChildLocationID {get; set;}

    [ForeignKey("ParentLocationID")]
    public Location Location1 {get; set;}
    [ForeignKey("ChildLocationID")]
    public Location Location2 {get; set;}
} 

Is this what you mean? 你是这个意思吗?

But how can I know in the code that ParentLocationID is using which navigation property Location1 or Location2? 但是我如何在代码中知道ParentLocationID正在使用哪个导航属性Location1或Location2?

You can rename the navigation properties in the EDXM Model Browser. 您可以在EDXM模型浏览器中重命名导航属性。 Just right click the navigation property and rename it so instead of Location1 , you can change it to ParentLocation . 只需右键单击导航属性并将其重命名,以便代替Location1 ,可以将其更改为ParentLocation

This will survive if you update the model from the database any number of times. 如果您多次更新数据库中的模型,这将继续存在。 However, if you totally delete the table from the EDMX and then re-create it, you will need to rename it again (but this is rarely done). 但是,如果您从EDMX中完全删除了该表,然后重新创建它,则需要再次对其重命名(但是很少这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM