Home >Backend Development >C++ >How to Handle Two Foreign Keys from the Same Table in Entity Framework Code First?

How to Handle Two Foreign Keys from the Same Table in Entity Framework Code First?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-29 07:59:38117browse

How to Handle Two Foreign Keys from the Same Table in Entity Framework Code First?

The physical framework code is preferred: handle two outer keys from the same table

When using the physical framework code to prioritize the relationship between the entity, you may encounter two external keys that need to be established from the same table. This is particularly challenging for beginners.

Suppose you want to create a data model for the team and the game. Each game involves two teams (home teams and guest teams). Using code preferred, you may write the following code:

However, this code will cause abnormalities:
<code class="language-csharp">public class Team
{
    [Key]
    public int TeamId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Match> Matches { get; set; }
}

public class Match
{
    [Key]
    public int MatchId { get; set; }

    [ForeignKey("HomeTeam"), Column(Order = 0)]
    public int HomeTeamId { get; set; }
    [ForeignKey("GuestTeam"), Column(Order = 1)]
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}</code>

This reference relationship will lead to unwanted cycle references. [Constraint name = match_guestteam]

In order to solve this problem, you need to provide different methods for EF to map the external keys. A effective solution is to create a separate collection for the home games and away games in the
class:

Team

In the class, you can remove the
<code class="language-csharp">public class Team
{
    public int TeamId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Match> HomeMatches { get; set; }
    public virtual ICollection<Match> AwayMatches { get; set; }
}</code>
attribute, just define the outer key attribute:

Match ForeignKey Finally, in the class, you need to use

and
<code class="language-csharp">public class Match
{
    public int MatchId { get; set; }

    public int HomeTeamId { get; set; }
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}</code>
configuration relationship:

DbContext HasRequired() Through these changes, you can successfully establish two external keys from the same table in the physical framework code priority model. WithMany()

The above is the detailed content of How to Handle Two Foreign Keys from the Same Table in Entity Framework Code First?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn