c# - Entity Framework 核心 : Fluent api many-to-many

标签 c# asp.net asp.net-core entity-framework-core

我如何建模以下内容:一个用户有很多关注者并关注很多用户。同一个用户有许多被阻止的用户(Twitter 有点功能)。

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<User> Following { get; set; }
    public virtual ICollection<User> Followers { get; set; }
    public virtual ICollection<User> BlockedUsers { get; set; }
}

public class User
{
    public ApplicationUser User { get; set; }
    public string UserId { get; set; }
    public ApplicationUser Follower { get; set; }
    public string FollowerId { get; set; }
}

到目前为止我的实现:
public void Configure(EntityTypeBuilder<User> builder)
{
    builder.HasKey(k => new { k.UserId, k.FollowerId });

    builder.HasOne(l => l.User)
           .WithMany(a => a.Followers)
           .HasForeignKey(l => l.UserId);

    builder.HasOne(l => l.Follower)
           .WithMany(a => a.Following)
           .HasForeignKey(l => l.FollowerId);
}

如何实现被阻止的用户字段?
public virtual ICollection<User> BlockedUsers { get; set; }

最佳答案

正如聊天中所讨论的,我很少相信 EF 的多对多功能,更不用说 EF Core。这不是直接针对您的问题,而是解释了如果这是我的项目我将如何处理。

您有您的 ApplicationUser已经,所以从它们出来的表只会存在来定义不同 ApplicationUsers 之间的关系。 .每个用户可以拥有多个所有内容:关注者、关注者和阻止。用户并不直接控制谁跟随他们,所以不需要自己的表。您可以通过查看关注者表来确定谁关注了用户。

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<UserFollow> Following { get; set; }
    public virtual ICollection<UserFollow> Followers { get; set; }
    public virtual ICollection<UserBlock> BlockedUsers { get; set; }
}

public class UserFollow
{
    public int Id { get; set; }

    [ForeignKey(nameof(SourceUserId))]
    public ApplicationUser SourceUser { get; set; }
    public string SourceUserId { get; set; }

    [ForeignKey(nameof(FollowedUserId))]
    public ApplicationUser FollowedUser { get; set; }
    public string FollowedUserId { get; set; }
}

public class UserBlock
{
    public int Id { get; set; }

    [ForeignKey(nameof(SourceUserId))]
    public ApplicationUser SourceUser { get; set; }
    public string SourceUserId { get; set; }

    [ForeignKey(nameof(BlockedUserId))]
    public ApplicationUser BlockedUser { get; set; }
    public string BlockedUserId { get; set; }
}

然后你的配置不会有太大变化(考虑这个伪,未经测试):
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    moderlBuilder.Entity<UserFollow>()
           .HasOne(l => l.SourceUser)
           .WithMany(a => a.Following)
           .HasForeignKey(l => l.SourceUserId);

    moderlBuilder.Entity<UserFollow>()
           .HasOne(l => l.FollowedUser)
           .WithMany(a => a.Followers)
           .HasForeignKey(l => l.FollowedUserId);

    moderlBuilder.Entity<UserBlock>()
           .HasOne(l => l.SourceUser)
           .WithMany(a => a.BlockedUsers)
           .HasForeignKey(l => l.SourceUserId);
}

(请注意,我总是敲击一个简单的键( Id 只是为了便于查询)但您可以根据需要将其改回复合键)

关于c# - Entity Framework 核心 : Fluent api many-to-many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582268/

相关文章:

c# - ZXing.Net.Mobile 未立即开始扫描

c# - 如何只获取 DataTable 中具有相同 ID 的最新条目?

c# - Radgrid 填充容器高度

asp.net - 如何从页面代码中的全局变量分配控件属性值?

c# - 在 C# 问题中执行 Powershell 脚本

c# - SHA256CryptoServiceProvider 和相关的可以在 WinXP 上使用吗?

javascript - 动态加载带有选项卡的表单

azure - ASP.NET Core token 获取异常

asp.net - 在自定义声明中保留用户安全配置文件数据

angular - asp.net core prerender webpack 错误 : TypeError: Cannot read property 'filter'