[英]How to load Entities with an defined Graph in EF Core?
我在EF Core中加載實體時遇到一些問題,不確定是否以錯誤的方式使用它,或者這是否是錯誤?
例如:
public class Root
{
public Contained ContainedReference { get; set; }
}
public class Contained
{
public Contained AnotherContainedReference { get; set; }
public System.Collections.Generic.ICollection<Root> Roots { get; set; } //Backreference
}
我的用於加載實體的代碼如下所示:
public override ICollection<Package> GetAll()
{
return Context.Set<Root>().Include(_=>_.ContainedReference);
}
就我而言,現在我想加載以下結構:Root.ContainedReference
我得到的是:Root.ContainedReference.AnotherContainedReference
我怎么能告訴EF Core它應該加載包含ContainedReference的Root,而不要包含ContainedReference中的AnotherContainedReference?
如果我現在嘗試更新(DBContext.Update(Root)),它將失敗,因為在對Contained.Roots和EF Core進行迭代時存在一個循環
無法跟蹤實體類型“ Root”的實例,因為已經跟蹤了另一個鍵值為“ {Id}”的實例。 附加現有實體時,請確保僅附加一個具有給定鍵值的實體實例。
由於存在反向引用(Roots in Contained,因此不應加載)
先感謝您
克里斯
我想我在https://github.com/aspnet/EntityFrameworkCore/issues/11564找到了答案
我只是清除導航屬性,現在它可以正常工作,但沒有最終解決方案。 也許他們會在不久的將來實現這種功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.