简体   繁体   English

在 EF 内核中急切加载 null 会出现错误

[英]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 获得的 nullChild有 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM