简体   繁体   English

实体框架包含两个外键

[英]Entity Framework Include with two foreign Keys

I have a Team table and a Matches table. 我有一个Team表和一个Matches表。

Team table: 团队表:

Id Name
------------
1  TeamA
2  TeamB

If TeamA desires to play TeamB we will add a row in the Matches table 如果TeamA想玩TeamB,我们将在Matches表中添加一行

Matches table: 匹配表:

Id HomeTeamId RivalTeamId
-------------------------
1       1          2

If TeamB desires to challenge TeamA we will go about do the following 如果TeamB想要挑战TeamA,我们将继续执行以下操作

Matches table: 匹配表:

Id   HomeTeamId   RivalTeamId
-----------------------------
 1       1             2
 2       2             1

My Team and Match POCO (only relevant code) look like this: 我的TeamMatch POCO(仅相关代码)如下所示:

public class Team : BaseEntity
{
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<Match> HomeMatches { get; set; }
        public ICollection<Match> RivalMatches { get; set; }
}

public class Match : BaseEntity
{
        public int Id { get; set; }
        public string Description { get; set; }

        //navs
        public Team HomeTeam { get; set; }
        public int HomeTeamId { get; set; }

        public Team RivalTeam { get; set; }
        public int RivalTeamId { get; set; }
}

Problem : I guess the experts can already notice a possibility of a cycle up there that I'll come across during query. 问题 :我想专家们已经注意到在查询过程中可能会出现循环的可能性。 My problem is that I would like to query TeamA's matches. 我的问题是我想查询TeamA的比赛。 I would do the following for that 为此,我将执行以下操作

 Team team= dbContext.Team.
                Include("HomeMatches").

In the above team object I notice that BOTH the properties HomeMatches and RivalMatches appear filled. 在上面的团队目标,我注意到, 无论是性能HomeMatchesRivalMatches出现填补。

I would just want the HomeMatches properties filled. 我只想填充HomeMatches属性。 I am only interested in those matches which TeamA has chosen to play, not in those where TeamA is a rival. 我只对TeamA选择参加的那些比赛感兴趣,而对TeamA是竞争对手的那些感兴趣。

My question is that when I am clearly mentioned Include("HomeMatches") , why is the RivalMatches property also filled? 我的问题是,当我明确提到Include("HomeMatches") ,为什么还要填充RivalMatches属性?

您可以尝试在此处使用显式加载

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

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