EF LINQ include multiple and nested entities

后端 未结 6 1154
猫巷女王i
猫巷女王i 2020-11-28 03:25

Ok, I have tri-leveled entities with the following hierarchy: Course -> Module -> Chapter

Here was the original EF LINQ statement:

Course course = db         


        
6条回答
  •  渐次进展
    2020-11-28 04:02

    In Entity Framework Core (EF.core) you can use .ThenInclude for including next levels.

    var blogs = context.Blogs
        .Include(blog => blog.Posts)
            .ThenInclude(post => post.Author)
        .ToList();
    

    More information: https://docs.microsoft.com/en-us/ef/core/querying/related-data

    Note: Say you need multiple ThenInclude() on blog.Posts, just repeat the Include(blog => blog.Posts) and do another ThenInclude(post => post.Other).

    var blogs = context.Blogs
        .Include(blog => blog.Posts)
            .ThenInclude(post => post.Author)
        .Include(blog => blog.Posts)
            .ThenInclude(post => post.Other)
     .ToList();
    

提交回复
热议问题