相关疑难解决方法(0)

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

用户友情关系

我找到了答案

实体框架核心:与同一实体的多对多关系, 并尝试这样做.

实体来说:

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; }
}
Run Code Online (Sandbox Code Playgroud)

流畅的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 => …
Run Code Online (Sandbox Code Playgroud)

c# many-to-many entity-framework-core

6
推荐指数
2
解决办法
2504
查看次数

标签 统计

c# ×1

entity-framework-core ×1

many-to-many ×1