Entity Framework Code First - two Foreign Keys from same table

Jarek

I've just started using EF code first, so I'm a total beginner in this topic.

I wanted to create relations between Teams and Matches:

1 match = 2 teams (home, guest) and result.

I thought it's easy to create such a model, so I started coding:

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

And I get an exception:

The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Match_GuestTeam ]

How can I create such a model, with 2 foreign keys to the same table?

Ladislav Mrnka

Try this:

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

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);
    }
}

Primary keys are mapped by default convention. Team must have two collection of matches. You can't have single collection referenced by two FKs. Match is mapped without cascading delete because it doesn't work in these self referencing many-to-many.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

Entity Framework Core - Code First - Two Foreign Keys - One Table

Entity Framework Core Two Foreign Keys - Same Table

entity framework Invalid column name Id (two foreign keys from the same primary table)

Defining multiple Foreign Key for the Same table in Entity Framework Code First

Entity Framework multiple foreign keys to the same table

MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths

Entity Framework (code first) creates two foreign key fields for the same property when using subclasses

FK to the Same Table Code First Entity Framework

Exchanged Foreign Key on Entity Framework Code First From data base

Two Foreign Keys in Entity Framework Core

MYSQL: select by two foreign keys from the same table

join on two foreign keys from same table in SQL

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint | ASP.NET Core & Entity Framework Core Code First

Entity Framework 6 Creating Two table from the same entity object

Entity Framework Code-first Many to many relationship on the same table

Foreign Key in Code First Entity Framework

Entity Framework Code First Foreign Key issue

Entity Framework - Code First - Foreign Key Constraint

Entity Framework Code First Foreign Key issue

Entity Framework: Foreign Key in code first

Code First Foreign Keys between two entities

Entity Framework Core code first many-to-many save with only foreign keys

How to configure Entity Framework Code First collections with strings as foreign keys to parent IDs to cascade delete?

Entity Framework with foreign keys

Two foreign keys pointing to the same table / model

JPA Hibernate two foreign keys to the same table

Mysql: using two foreign keys to the same table

Specify ON DELETE NO ACTION create 3 foreign keys tha same table - Entity Framework