亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號