两个具有相同导航属性的外键? [英] Two foreign keys with same Navigation Property?

查看:156
本文介绍了两个具有相同导航属性的外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Entity Framework的新手,所以我不太了解它。目前,我在我的学院项目工作,在那个项目我遇到一个问题,我有两个外键指的是另一个表中的同一列。我该怎么处理这种情况。

I am new to Entity Framework so I don't know much about it. Currently I am working on My College Project, in that Project I came across a problem where I have two foreign keys refers to the Same column in another table. how can I handle this situation.

是否需要为每个外键创建导航属性。如果我为ContactId创建另一个Navigaton属性,则需要在User类中创建另一个导航属性,如:

Is it necessary to create Navigation Property for Every Foreign key. And if I create another Navigaton property for ContactId then it is necessary to create another Navigation Property in User class like:

public virtual ICollection<BlockedUser> SomePropertyName { get; set; }

请告诉我解决这个问题的最佳方法。我正在使用实体框架6。

please tell me the best way to overcome this problem. I am using Entity Framework 6.

以下是我的模型类:

public class BlockedUser
{
    // User Foreign Key
    public int UserId { get; set; }               // Composite Primary Key

    // User Foreign key
    public int ContactId { get; set; }            // Composite Primary Key

    // User Navigation Property
    public virtual User User { get; set; }
}

public class User
{
    public int UserId { get; set; }  // Primary key
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    // BlockedUser Navigation Property
    public virtual ICollection<BlockedUser> BlockedUsers { get; set; }
}


推荐答案


是否需要为每个外键创建导航属性?

Is it necessary to create Navigation Property for Every Foreign key?

是,或者更准确地说:您需要至少一个导航属性对于每个关系。 至少有一个表示您可以决定要将导航属性添加到的两个实体中的哪一个。如果您经常想要从实体A导航到实体B,或者其他方式,则通常取决于应用程序中最常见的用例。如果需要,您可以将导航属性添加到两个实体,但不需要。

Yes, or more precisely: You need at least one navigation property for every relationship. "At least one" means that you can decide which of the two entities you want to add the navigation property to. It normally depends on the most common use cases in your application if you often want to navigate from entity A to entity B or the other way around. If you want, you can add the navigation properties to both entities but you don't need to.

在您的模型中,您显然有两个(一对多)关系。如果要在两个实体中显示导航属性,您将需要四个导航属性,并且 - 重要! - 您必须为关系定义哪一个导航属性(见以下代码片段中的 [InverseProperty] 属性)。

In your model you apparently have two (one-to-many) relationships. If you want to expose navigation properties in both entities you would need four navigation property and - important! - you have to define which navigation properties form a pair for a relationship (see the [InverseProperty] attribute in the following code snippet).

使用数据注释,它会这样:

With data annotations it would like this:

public class BlockedUser
{
    [Key, ForeignKey("User"), Column(Order = 1)]
    public int UserId { get; set; }

    [Key, ForeignKey("Contact"), Column(Order = 2)]
    public int ContactId { get; set; }

    [InverseProperty("BlockedUsers")]
    public virtual User User { get; set; }

    [InverseProperty("BlockedContacts")]
    public virtual User Contact { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public virtual ICollection<BlockedUser> BlockedUsers { get; set; }
    public virtual ICollection<BlockedUser> BlockedContacts { get; set; }
}

如果你不想要 BlockedContacts collection可以从联系人 [InverseProperty(BlockedContacts)] 属性$ c>导航属性。

If you don't want the BlockedContacts collection you can probably just remove it and the [InverseProperty("BlockedContacts")] attribute from the Contact navigation property as well.

这篇关于两个具有相同导航属性的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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