簡體   English   中英

在 EF 內核中急切加載 null 會出現錯誤

[英]Eagerloading a null in EF core gives an error

Include 獲得的 nullChild有 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM