2

I have encountered a problem when using EF Core 2.2.6. I have one Entity called Player and I want to reference another list of players, eg. Friends for example.

I'm doing it with Join table and entity called FriendPlayer.

public class FriendPlayer
    {
        public Guid PlayerId { get; set; }

        public Player Player { get; set; }

        public Guid FriendId { get; set; }

        public Player Friend { get; set; }
    }

and configured like this

modelBuilder.Entity<FriendPlayer>()
  .HasOne(fp => fp.Friend)
  .WithMany(p => p.Friends);

In Player entity it is simply this:

public ICollection<FriendPlayer> Friends { get; set; } = new List<FriendPlayer>();

The problem is, that when I try to create this new relation between two players and save it, I ended up with same Guid for the FriendPlayer entity in both ID properties. I guess that same type of referencing entity might be the problem, but couldn't find anything in docs that would confirm that. Every tutorial with many-to-many is using two different entities in join class, not same type and I thought that this couldn't be really some exception, because I've already solved some similar scenarios in the past.

Thank you for any advice.

Ondra Piza
  • 135
  • 1
  • 12

0 Answers0