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

Diskdrive

So I'm following this answer in trying to get two foreign keys to get to a single table and it works.

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

However, in my solution I don't want Team to have HomeMatches and AwayMatches collections as it doesn't make sense to be able to navigate to Match from that entity.

Is it possible to have two foreign keys pointing to the same table when the entity for the parent table doesn't have collections for the child tables.

I would like my Team entity to be like below.

public class Team
{
    public int TeamId { get; set;} 
    public string Name { get; set; }
    // HomeMatches and AwayMatches collection is no longer here
}

How could I use the modelBuilder to articulate to EntityFramework that I want to HomeTeamID and GuestTeamID to be foreign keys of Team?

Lanorkin

Just remove collections & leave empty parameters for .WithMany().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Howto specify table name with Entity Framework Code First Fluent API

Entity Framework Code First - two Foreign Keys from same table

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

FK to the Same Table Code First Entity Framework

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

Entity Framework 6 Creating Two table from the same entity object

Entity Framework Core Two Foreign Keys - Same Table

Two Foreign Keys in Entity Framework Core

Entity Framework with foreign keys

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

two foreign keys on same table to display parent fields

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

Entity Framework Migration Inheritance without creating the parent table

Having two classes representing the same table in Entity Framework?

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

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

Get foreign key value using Entity Framework code first

Multiple foreign keys to the same parent table

Entity Framework Code First Self Referencing Parent Child with Payload

Foreign Key in Code First Entity Framework

Entity Framework multiple foreign keys to the same table

asp.net mvc - Inserting multiple records in the third table having foreign keys to parent using entity framework 6

How to get parent entity property in child entity with foreign key relation in code first?

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

How to specify a list of foreign keys in Entity Framework?

How to specify a foreign key with code-first Entity Framework

Exchanged Foreign Key on Entity Framework Code First From data base

Entity Design with a table having only foreign keys

How to join two tables using Entity Framework and match foreign keys