Entity Framework Core:Fluent api 多对多

我如何建模以下内容:一个用户有很多关注者并关注很多用户。同一个用户有许多被阻止的用户(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; }


ibeautiful
浏览 181回答 1
1回答

偶然的你

正如聊天中所讨论的,我很少相信 EF 的多对多功能,更不用说 EF Core。这不是直接针对您的问题,而是解释了如果这是我的项目我将如何处理。您已经拥有ApplicationUser了,因此从它们中分离出来的表只会存在于定义不同ApplicationUsers. 每个用户可以拥有多个所有内容:关注者、关注者和阻止。用户并不直接控制谁跟随他们,所以不需要自己的表。您可以通过查看关注者表来确定谁关注了用户。public class ApplicationUser : IdentityUser{&nbsp; &nbsp; public virtual ICollection<UserFollow> Following { get; set; }&nbsp; &nbsp; public virtual ICollection<UserFollow> Followers { get; set; }&nbsp; &nbsp; public virtual ICollection<UserBlock> BlockedUsers { get; set; }}public class UserFollow{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; [ForeignKey(nameof(SourceUserId))]&nbsp; &nbsp; public ApplicationUser SourceUser { get; set; }&nbsp; &nbsp; public string SourceUserId { get; set; }&nbsp; &nbsp; [ForeignKey(nameof(FollowedUserId))]&nbsp; &nbsp; public ApplicationUser FollowedUser { get; set; }&nbsp; &nbsp; public string FollowedUserId { get; set; }}public class UserBlock{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; [ForeignKey(nameof(SourceUserId))]&nbsp; &nbsp; public ApplicationUser SourceUser { get; set; }&nbsp; &nbsp; public string SourceUserId { get; set; }&nbsp; &nbsp; [ForeignKey(nameof(BlockedUserId))]&nbsp; &nbsp; public ApplicationUser BlockedUser { get; set; }&nbsp; &nbsp; public string BlockedUserId { get; set; }}然后你的配置不会有太大变化(考虑这个伪,未经测试):protected override void OnModelCreating(ModelBuilder modelBuilder){&nbsp; &nbsp; moderlBuilder.Entity<UserFollow>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasOne(l => l.SourceUser)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.WithMany(a => a.Following)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasForeignKey(l => l.SourceUserId);&nbsp; &nbsp; moderlBuilder.Entity<UserFollow>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasOne(l => l.FollowedUser)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.WithMany(a => a.Followers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasForeignKey(l => l.FollowedUserId);&nbsp; &nbsp; moderlBuilder.Entity<UserBlock>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasOne(l => l.SourceUser)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.WithMany(a => a.BlockedUsers)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.HasForeignKey(l => l.SourceUserId);}(请注意,我总是使用一个简单的键(Id只是为了便于查询),但您可以根据需要将其改回复合键)
打开App,查看更多内容
随时随地看视频慕课网APP