我有两个对象:用户和关系。为我系统的每个用户创建一个用户对象,并且将为每个用户创建多个关系,具体取决于他们添加为朋友的人数。对象如下所示
public class User : IdentityUser {}
public class Relationship {
// User who created the relationship
public User User {get;set;}
// User who the creation user is friends with
public User Friend {get;set;}
// Enum of Status (Approved, Pending, Denied, Blocked)
public RelationshipStatus RelationshipStatus {get;set;}
public DateTime RelationshipCreationDate {get;set;}
}
每个用户可以有多个匹配的记录,Relationship.User也可以有多个匹配的记录Relationship.Friend。我的 DbContext 设置如下OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Relationship>(relationship =>
{
relationship.HasOne(r => r.User).WithMany(u => u.Friends);
});
}
我是 Entity 的新手,所以这可能是微不足道的 - 我缺少什么来设置我所描述的关系?谢谢!
编辑:更具体地说,这是我得到的错误:
InvalidOperationException: Unable to determine the relationship represented by navigation property 'Relationship.Friend' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
编辑 2:在重新考虑我的流程后,我采取了不同的方法,并使用关系来包括追随者和追随者,而不是集体朋友列表(无论如何更适合我的商业模式)。我通过以下方式实现了这一点:
public class User : IdentityUser {
[InverseProperty("Friend")]
public List<Relationship> Followers {get;set;}
[InverseProperty("User")]
public List<Relationship> Following {get;set;}
}
我很好奇是否有人在我的解决方案之外有解决原始问题的方法,但是,这目前有效,似乎最适合我。
拉莫斯之舞
慕容森
相关分类