[英]Eagerloading a null in EF core gives an error
當Include 獲得的 null的Child有 ThenInclude 時,我們如何處理場景?
_context.Bakery
.Include(p=>p.Parent) //Parent is null so there is an exception
.ThenInclude(c=>c.Child)
.SingleOrDefaultAsync();
在面包店 class 中:
public class Bakery
{
public virtual SomeParent Parent {get;set;}
}
在 SomeParent class 中:
public class SomeParent
{
public virtual SomeChildren Child {get;set;}
}
只需添加容錯運算符“!” 你的第一個包括
_context.Bakery
.Include(p=>p.Parent!) // <--
.ThenInclude(c=>c.Child)
.SingleOrDefaultAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.