Home > Backend Development > C++ > How to Model a Team-Match Relationship with Two Foreign Keys from the Same Table in Entity Framework Code First?

How to Model a Team-Match Relationship with Two Foreign Keys from the Same Table in Entity Framework Code First?

Barbara Streisand
Release: 2025-01-29 08:04:09
Original
774 people have browsed it

How to Model a Team-Match Relationship with Two Foreign Keys from the Same Table in Entity Framework Code First?

Using physical framework code Preferred: Create two external keys from the same table to indicate the team matching relationship

Question:

When a beginner studys the complexity of the physical framework code, he sought guidance to define an effective model to reflect the relationship between the team and the game. The specific requirements are that each game involves two different team entities, which are specified as home teams and guest teams, and retain the results of the game.

Answer:

You can use the following methods to build the required model:

Rebcing the team class attributes:

Remove the ICOLLECTION property of the existing Matches from the Team class.

Introduce two new iCollection properties: Homematches and AwayMatches.
  1. Modify the model as follows:
  2. Custom outer key relationship:
<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>
Copy after login

Modify the MATCH class to contain the necessary key attributes, and remove the attribute from the Hometeam and Guestteam properties.

Use Fluent API in DBCONTEX to define the external key relationship:
  1. [ForeignKey]
  2. Note:
<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; }
}

public class Context : DbContext
{
    // ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Match>()
                    .HasRequired(m => m.HomeTeam)
                    .WithMany(t => t.HomeMatches)
                    .HasForeignKey(m => m.HomeTeamId)
                    .WillCascadeOnDelete(false);

        modelBuilder.Entity<Match>()
                    .HasRequired(m => m.GuestTeam)
                    .WithMany(t => t.AwayMatches)
                    .HasForeignKey(m => m.GuestTeamId)
                    .WillCascadeOnDelete(false);
    }
}</code>
Copy after login
Each Team entity must have two matches, Homematches and AwayMatches.

Use Fluent API to define the external key relationship, because in this scene, the default agreed by the mapping main key is insufficient.

Disable the delete of the class to prevent accidental deleting the Teams referenced by multiple matches.
  • This Revised Answer Maintains The Original Image and Ueses More Concise and Clearer Language While Retaining The Core Information. Ed for better readability.

The above is the detailed content of How to Model a Team-Match Relationship with 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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template