Search code examples
.net-coreentity-framework-coredomain-driven-designentity-framework-migrationsef-core-5.0

Both relationships could use MatchId as foreign key


I have a problem with EF Core code first class object mapping. I have a class Match where I have properties for Team A and Team B.

public class Match: MyEntity
    {
        public virtual Team TeamA { get; private set; }

        public virtual Team TeamB { get; private set; }

        public GameType GameType { get; private set; }

        public MatchStatus Status { get; private set; }

        public Match()
        {
 
        }

Here my Team entity, where I have reference on MatchId.

    public class Team: MyEntity
    {
        public virtual int MatchId { get; private set; }

        private Team()
        { }
        ...
    }

So I need MatchId be the same for TeamA and TeamB in case they are in the same match. When I am trying to create migration, I have an error:

Both relationships between 'Team' and 'Match.TeamA' and between 'Team' and 'Match.TeamB' could use {'MatchId'} as the foreign key. To resolve this, configure the foreign key properties explicitly in 'OnModelCreating' on at least one of the relationships.


 builder.Entity<Team>()
.HasOne<Match>()
.WithOne(x => x.TeamA)
.HasForeignKey<Team>(x => x.MatchId);
  
builder.Entity<Team>()
.HasOne<Match>()
.WithOne(x => x.TeamB)
.HasForeignKey<Team>(x => x.MatchId); 

When I am using this configuration it works, but it is deleting in migration TeamAId and TeamBId columns from Matches table and creates two columns in Team table: MatchId and MatchId1.

      migrationBuilder.DropColumn(
                name: "TeamAId",
                table: "Matches");

            migrationBuilder.DropColumn(
                name: "TeamBId",
                table: "Matches");

            migrationBuilder.AddColumn<int>(
                name: "MatchId",
                table: "Teams",
                type: "int",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "MatchId1",
                table: "Teams",
                type: "int",
                nullable: true);

Maybe am I missing something?

How should I fix this ?

Thank you for any help.


Solution

  • I've fixed it with not relying on default EF Core configurations, but on mine in OnModelCreating. Hope I did it in right way, someone can tell if see something wrong.

     builder.Entity<Team>(b =>
                {
                    b.HasOne<Match>()
                        .WithOne(x => x.TeamA)
                        .HasForeignKey<Team>(x => x.MatchId);
    
                    b.HasOne<Match>()
                        .WithOne(x => x.TeamB)
                        .HasForeignKey<Team>(x => x.MatchId);
                });
    
       builder.Entity<Match>(b =>
                {
                       b.HasOne<Team>("TeamA")
                      .WithMany()
                        .HasForeignKey("TeamAId");
    
                    b.HasOne<Team>("TeamB")
                        .WithMany()
                        .HasForeignKey("TeamBId");
    
                    b.Navigation("TeamA");
    
                    b.Navigation("TeamB");
                });