Search code examples
entity-frameworkmany-to-manyef-core-2.0

EF Core 2.0 migration - Many-to-Many with additional fields


I'm using EF Core 2.0 and created a many-to-many relationship with a join entity. When I add a new migration EF always creates an additional Index/Id-field which is completely stupid. Here is my join entity:

public class Team_Member
{
    public int TeamId { get; set; }
    public Team Team { get; set; }

    public int MemberId { get; set; }
    public Member Member { get; set; }

    public MemberTypeEnum MemberType { get; set; }
}

This is the configuration of the join table (following several examples on the internet):

public class Team_MemberConfig : IEntityTypeConfiguration<Team_Member>
{
    public void Configure(EntityTypeBuilder<Team_Member> builder)
    {
        builder.ToTable("Team_Member");
        builder.HasKey(tm => new { tm.TeamId, tm.MemberId });
        builder.HasOne<Team>()
            .WithMany(t => t.Team_Member)
            .HasForeignKey(tm => tm.TeamId)
            .OnDelete(DeleteBehavior.Restrict);
        builder.HasOne<Member>()
            .WithMany(m => m.Team_Member)
            .HasForeignKey(tm => tm.MemberId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}

For every foreign key columns migration adds a second one:

columns: table => new
            {
                TeamId = table.Column<int>(nullable: false),
                MemberId = table.Column<int>(nullable: false),
                **MemberId1** = table.Column<int>(nullable: true),
                MemberType = table.Column<int>(nullable: false),
                **TeamId1** = table.Column<int>(nullable: true)
            },

and two constraints which are absolutely identical:

table.ForeignKey(
                    name: "FK_Team_Member_Member_MemberId",
                    column: x => x.MemberId,
                    principalTable: "Member",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Team_Member_Member_MemberId1",
                    column: x => x.MemberId1,
                    principalTable: "Member",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Team_Member_Team_TeamId",
                    column: x => x.TeamId,
                    principalTable: "Team",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Team_Member_Team_TeamId1",
                    column: x => x.TeamId1,
                    principalTable: "Team",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);

What am I doing wrong here?


Solution

  • Anytime you see EF (Core) generating additional FK columns, it's clear indication of unmapped reference or collection navigation property caused by the usage of a wrong fluent API overload. All related fluent API (HasOne, HasMany, WithOne, WithMany) have overloads with and w/o navigation property. You must use the first when the corresponding entity has no navigation And you should use the exact one representing the presence/absence of a navigation property in the corresponding entity. Failing to do so leaves unmapped navigation properties and EF creates additional relationships.

    In your case, the two discrepancies (marked as A, B) are between:

    public class Team_Member
    {
        public int TeamId { get; set; }
        public Team Team { get; set; } // <-- A
    
        public int MemberId { get; set; }
        public Member Member { get; set; } // <-- B
    
        public MemberTypeEnum MemberType { get; set; }
    }
    

    and

    builder.HasOne<Team>() // <-- A
        .WithMany(t => t.Team_Member)
        .HasForeignKey(tm => tm.TeamId)
        .OnDelete(DeleteBehavior.Restrict);
    builder.HasOne<Member>() // <-- B
        .WithMany(m => m.Team_Member)
        .HasForeignKey(tm => tm.MemberId)
        .OnDelete(DeleteBehavior.Restrict);
    

    As mentioned earlier, simply make them match:

    builder.HasOne(tm => tm.Team) // <-- A
        .WithMany(t => t.Team_Member)
        .HasForeignKey(tm => tm.TeamId)
        .OnDelete(DeleteBehavior.Restrict);
    builder.HasOne(tm => tm.Member) // <-- B
        .WithMany(m => m.Team_Member)
        .HasForeignKey(tm => tm.MemberId)
        .OnDelete(DeleteBehavior.Restrict);
    

    and the issue is gone.