简体   繁体   English

EF Core - 课堂上的多对多关系

[英]EF Core - Many to many relationship on a class

User-Friend relationship 用户友情关系

I find an answer 我找到了答案

Entity Framework Core: many-to-many relationship with same entity and try like this. 实体框架核心:与同一实体的多对多关系,并尝试这样做。

Entitys: 实体来说:

public class User
{
    public int UserId { get; set; }

    public virtual ICollection<Friend> Friends { get; set; }
}

public class Friend
{
    public int MainUserId { get; set; }

    public User ManUser { get; set; }

    public int FriendUserId { get; set; }

    public User FriendUser { get; set; }
}

The fluent API: 流畅的API:

modelBuilder.Entity<Friend>()
    .HasKey(f => new { f.MainUserId, f.FriendUserId });

modelBuilder.Entity<Friend>()
    .HasOne(f => f.ManUser)
    .WithMany(mu => mu.Friends)
    .HasForeignKey(f => f.MainUserId);

modelBuilder.Entity<Friend>()
    .HasOne(f => f.FriendUser)
    .WithMany(mu => mu.Friends)
    .HasForeignKey(f => f.FriendUserId);

When I Add-Migration, the error message is 当我添加迁移时,错误消息是

Cannot create a relationship between 'User.Friends' and 'Friend.FriendUser', because there already is a relationship between 'User.Friends' and 'Friend.ManUser'. 无法在'User.Friends'和'Friend.FriendUser'之间创建关系,因为'User.Friends'和'Friend.ManUser'之间已经存在关系。 Navigation properties can only participate in a single relationship. 导航属性只能参与单个关系。

What should I do? 我该怎么办? Or I should create an Entity FriendEntity:User? 或者我应该创建一个Entity FriendEntity:User?

The problem is that you can't have one collection to support both one-to-many associations. 问题是你不能有一个集合来支持一对多的关联。 Friend has two foreign keys that both need an inverse end in the entity they refer to. Friend有两个外键,它们都需要在它们引用的实体中使用反向结尾 So add another collection as inverse end of MainUser : 所以添加另一个集合作为MainUser反向结束:

public class User
{
    public int UserId { get; set; }
    public virtual ICollection<Friend> MainUserFriends { get; set; }
    public virtual ICollection<Friend> Friends { get; set; }
}

And the mapping: 和映射:

modelBuilder.Entity<Friend>()
    .HasKey(f => new { f.MainUserId, f.FriendUserId });

modelBuilder.Entity<Friend>()
    .HasOne(f => f.MainUser)
    .WithMany(mu => mu.MainUserFriends)
    .HasForeignKey(f => f.MainUserId).OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Friend>()
    .HasOne(f => f.FriendUser)
    .WithMany(mu => mu.Friends)
    .HasForeignKey(f => f.FriendUserId);

One (or both) of the relationships should be without cascading delete to prevent multiple cascade paths. 一个(或两个)关系应该没有级联删除以防止多个级联路径。

It's not mandatory the second collection. 第二个系列不是强制性的。 You only need to left de .WithMany() empty like this: 你只需要像这样将de .WithMany()留空:

modelBuilder.Entity<Friend>()
    .HasOne(f => f.MainUser)
    .WithMany()
    .HasForeignKey(f => f.MainUserId);

modelBuilder.Entity<Friend>()
    .HasOne(f => f.FriendUser)
    .WithMany()
    .HasForeignKey(f => f.FriendUserId);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM