[英]Eagerloading a null in EF core gives an error
How can we handle scenarios when the Include is getting a null that has a ThenInclude for its Child ?当Include 获得的 null的Child有 ThenInclude 时,我们如何处理场景?
_context.Bakery
.Include(p=>p.Parent) //Parent is null so there is an exception
.ThenInclude(c=>c.Child)
.SingleOrDefaultAsync();
Here in the Bakery class:在面包店 class 中:
public class Bakery
{
public virtual SomeParent Parent {get;set;}
}
Here in the SomeParent class:在 SomeParent class 中:
public class SomeParent
{
public virtual SomeChildren Child {get;set;}
}
Just add null-forgiving operator "!"只需添加容错运算符“!” to your first include
你的第一个包括
_context.Bakery
.Include(p=>p.Parent!) // <--
.ThenInclude(c=>c.Child)
.SingleOrDefaultAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.