Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core EntityFramework核心,包括结果中的多级导航属性_Entity Framework Core - Fatal编程技术网

Entity framework core EntityFramework核心,包括结果中的多级导航属性

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.

在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.            .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. ...)