为了账号安全,请及时绑定邮箱和手机立即绑定

Entity Framework Core 无法确定关系

Entity Framework Core 无法确定关系

C#
jeck猫 2022-06-18 17:43:55
我有两个对象:用户和关系。为我系统的每个用户创建一个用户对象,并且将为每个用户创建多个关系,具体取决于他们添加为朋友的人数。对象如下所示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;}}我很好奇是否有人在我的解决方案之外有解决原始问题的方法,但是,这目前有效,似乎最适合我。
查看完整描述

2 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

您是否尝试向用户类添加关系列表,即


   public class User : IdentityUser {

       List<Relationship> Relationships {get;set;}

   }

这可能有助于 EF 充分理解这种关系。


查看完整回答
反对 回复 2022-06-18
?
慕容森

TA贡献1853条经验 获得超18个赞

看起来您正在尝试many-to-many使用相同的实体进行配置。为此,请编写您的User和Relationship模型类,如下所示:


public class User : IdentityUser 

{

   public List<Relationship> UserFriends { get; set; }

   public List<Relationship> FriendUsers { get; set; }

}


public class Relationship {


   public string UserId {get; set;} // `UserId` type is string as you are using default identity

   public User User {get;set;}


   public string FriendId {get; set;}

   public User Friend {get;set;}


   // Enum of Status (Approved, Pending, Denied, Blocked)

   public RelationshipStatus RelationshipStatus {get;set;}


   public DateTime RelationshipCreationDate {get;set;}

}

然后在模型构建器实体配置中:


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

   base.OnModelCreating(modelBuilder);


    modelBuilder.Entity<Relationship>().HasKey(r => new {r.UserId, r.FriendId});


    modelBuilder.Entity<Relationship>()

    .HasOne(r => r.User)

    .WithMany(u => u.UserFriends)

    .HasForeignKey(r => r.UserId).OnDelete(DeleteBehavior.Restrict);


    modelBuilder.Entity<Relationship>()

    .HasOne(r => r.Friend)

    .WithMany(f => f.FriendUsers)

    .HasForeignKey(r => r.FriendId).OnDelete(DeleteBehavior.Restrict);

}

现在它应该可以正常工作了!


查看完整回答
反对 回复 2022-06-18
  • 2 回答
  • 0 关注
  • 145 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信