EF Core - Many to many relationship on a class

后端 未结 2 532
無奈伤痛
無奈伤痛 2020-12-18 06:07

User-Friend relationship

I find an answer

Entity Framework Core: many-to-many relationship with same entity and try like this.

Entitys:

         


        
2条回答
  •  粉色の甜心
    2020-12-18 06:54

    It's not mandatory the second collection. You only need to left de .WithMany() empty like this:

    modelBuilder.Entity()
        .HasOne(f => f.MainUser)
        .WithMany()
        .HasForeignKey(f => f.MainUserId);
    
    modelBuilder.Entity()
        .HasOne(f => f.FriendUser)
        .WithMany()
        .HasForeignKey(f => f.FriendUserId);
    

    look at this : https://github.com/aspnet/EntityFramework/issues/6052

提交回复
热议问题