【发布时间】:2018-11-07 23:39:35
【问题描述】:
我如何建模以下内容:一个用户有很多关注者并关注了很多用户。同一个用户有很多被屏蔽的用户(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; }
【问题讨论】:
-
你能解释一下为什么你有
User和ApplicationUser吗?User类的用途是什么? -
User似乎是一个代表关注者/关注链接的多对多链接实体。因此,对于 Blocked / Blocking 链接,您需要一个 separate 类似的链接实体。 -
@DavidKemp 就像 Ivan 说的,用户是一个链接,它就像一个 join 表。 ApplicationUser 是一个扩展 IdentyUser 并向 AspNetUsers 表添加额外属性(列)的类。
-
这可能是审查文档的工作:docs.microsoft.com/en-us/ef/core/modeling/…,但就像@IvanStoev 所说,你需要另一个
class。
标签: c# asp.net asp.net-core entity-framework-core