![](/img/trans.png)
[英]EntityFramework Core: Eager loading navigation properties of derived types
[英]EntityFramework Core including multilevel navigation properties in results
在 EF Core 中,我們可以使用.Include
和.ThenInclude
方法在查詢中加載相關數據。 我們以官方文檔中的例子為例:
1. using (var context = new BloggingContext())
2. {
3. var blogs = context.Blogs
4. .Include(blog => blog.Posts)
5. .ThenInclude(post => post.Author)
6. .ThenInclude(author => author.Photo)
7. .Include(blog => blog.Owner)
8. .ThenInclude(owner => owner.Photo)
9. .ToList();
10.}
在上面的例子,它包括在Post.Author
屬性,然后Author.Photo
使用屬性ThenInclude
在線路5和6。
但是如果Post
實體有另一個我想要包含的導航屬性怎么辦? 如果我在第 6 行之后使用ThenInclude
,它將相對於Photo
屬性,如果我使用Include
,它將相對於Blogs
屬性。 有什么辦法可以直接在查詢語句中解決這個問題嗎?
您可以隨心所欲地重復相同的Include
s(並且認為是明智的):
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.Include(blog => blog.Posts)
.ThenInclude(post => post.Document)
.Include(blog => blog.Posts)
.ThenInclude(post => post. ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.