簡體   English   中英

在Entity Framework Core中加載多個級別的相關數據無法正常工作

[英]Loading multiple levels of related data in Entity Framework Core not working as expected

我想熱切地加載一些與Article對象相關的數據。

return await context.Articles
    .Include(x => x.ArticleTags)
        .ThenInclude(x => x.Tag)
            // .ThenInclude(x => x.Value)
    .Include(x => x.Author)
        .ThenInclude(x => x.UserInfo)
    .Include(x => x.Approver)
        .ThenInclude(x => x.UserInfo)
    .Include(x => x.Rejecter)
        .ThenInclude(x => x.UserInfo)
    .Include(x => x.LinkSubmitter)
        .ThenInclude(x => x.UserInfo)
    .FirstOrDefaultAsync(x => x.Id == id);

在本文對象中,所有顯示的屬性可以為null或為空。 eg no tags, no author, no approver .. etc.

該查詢按原樣工作(帶有注釋行)。

但是,如果我包括嵌套的ThenInclude() ,它將失敗並顯示錯誤:

System.ArgumentNullException:值不能為null。 參數名稱:來源

我相信這樣做的原因是因為該文章沒有標簽,並且正在努力加載不存在的標簽的值。 我如何急切地加載商品可能存在或可能不存在的所有標簽數據?

嘗試.Include(x => x.ArticleTags.Tag?.Value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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