Entity framework core EntityFramework核心,包括结果中的多级导航属性
在EF-Core中,我们可以使用Entity framework core EntityFramework核心,包括结果中的多级导航属性,entity-framework-core,Entity Framework Core,在EF-Core中,我们可以使用、Include和。然后Include方法在查询中加载相关数据。让我们以官方文档中的示例为例: 1. using (var context = new BloggingContext()) 2. { 3. var blogs = context.Blogs 4. .Include(blog => blog.Posts) 5. .ThenInclude(post => post.Author) 6.
、Include
和。然后Include
方法在查询中加载相关数据。让我们以官方文档中的示例为例:
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
属性,然后在第5行和第6行中使用thenclude
包含Author.Photo
属性
但是如果
Post
实体有另一个我想包含的导航属性怎么办?如果我在第6行之后使用Include
,它将与Photo
属性相关,如果我使用Include
它将与Blogs
属性相关。有没有办法直接在查询语句中解决这个问题?您可以随时重复相同的Include
s(并且认为合理):
我总是只.Include(x=>x.SomeProperty.PropertyA).Include(x=>x.SomeProperty.PropertyB).Include(x=>x.SomeCollection.Select(y=>y.PropertyC))
等(但不确定这在Core中是允许的,它在6x中)
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. ...)