猿问

Entity Framework Core 无法确定关系

我有两个对象:用户和关系。为我系统的每个用户创建一个用户对象,并且将为每个用户创建多个关系,具体取决于他们添加为朋友的人数。对象如下所示


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;}

}

我很好奇是否有人在我的解决方案之外有解决原始问题的方法,但是,这目前有效,似乎最适合我。


jeck猫
浏览 146回答 2
2回答

拉莫斯之舞

您是否尝试向用户类添加关系列表,即&nbsp; &nbsp;public class User : IdentityUser {&nbsp; &nbsp; &nbsp; &nbsp;List<Relationship> Relationships {get;set;}&nbsp; &nbsp;}这可能有助于 EF 充分理解这种关系。

慕容森

看起来您正在尝试many-to-many使用相同的实体进行配置。为此,请编写您的User和Relationship模型类,如下所示:public class User : IdentityUser&nbsp;{&nbsp; &nbsp;public List<Relationship> UserFriends { get; set; }&nbsp; &nbsp;public List<Relationship> FriendUsers { get; set; }}public class Relationship {&nbsp; &nbsp;public string UserId {get; set;} // `UserId` type is string as you are using default identity&nbsp; &nbsp;public User User {get;set;}&nbsp; &nbsp;public string FriendId {get; set;}&nbsp; &nbsp;public User Friend {get;set;}&nbsp; &nbsp;// Enum of Status (Approved, Pending, Denied, Blocked)&nbsp; &nbsp;public RelationshipStatus RelationshipStatus {get;set;}&nbsp; &nbsp;public DateTime RelationshipCreationDate {get;set;}}然后在模型构建器实体配置中:protected override void OnModelCreating(ModelBuilder modelBuilder){&nbsp; &nbsp;base.OnModelCreating(modelBuilder);&nbsp; &nbsp; modelBuilder.Entity<Relationship>().HasKey(r => new {r.UserId, r.FriendId});&nbsp; &nbsp; modelBuilder.Entity<Relationship>()&nbsp; &nbsp; .HasOne(r => r.User)&nbsp; &nbsp; .WithMany(u => u.UserFriends)&nbsp; &nbsp; .HasForeignKey(r => r.UserId).OnDelete(DeleteBehavior.Restrict);&nbsp; &nbsp; modelBuilder.Entity<Relationship>()&nbsp; &nbsp; .HasOne(r => r.Friend)&nbsp; &nbsp; .WithMany(f => f.FriendUsers)&nbsp; &nbsp; .HasForeignKey(r => r.FriendId).OnDelete(DeleteBehavior.Restrict);}现在它应该可以正常工作了!
随时随地看视频慕课网APP
我要回答