簡體   English   中英

實體框架包含兩個外鍵

[英]Entity Framework Include with two foreign Keys

我有一個Team表和一個Matches表。

團隊表:

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

如果TeamA想玩TeamB,我們將在Matches表中添加一行

匹配表:

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

如果TeamB想要挑戰TeamA,我們將繼續執行以下操作

匹配表:

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

我的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; }
}

問題 :我想專家們已經注意到在查詢過程中可能會出現循環的可能性。 我的問題是我想查詢TeamA的比賽。 為此,我將執行以下操作

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

在上面的團隊目標,我注意到, 無論是性能HomeMatchesRivalMatches出現填補。

我只想填充HomeMatches屬性。 我只對TeamA選擇參加的那些比賽感興趣,而對TeamA是競爭對手的那些感興趣。

我的問題是,當我明確提到Include("HomeMatches") ,為什么還要填充RivalMatches屬性?

您可以嘗試在此處使用顯式加載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM