简体   繁体   English

在Entity Framework Core中选择多个嵌套级别的子表

[英]Select multiple nested levels of child tables in Entity Framework Core

I want to get multiple nested levels of child tables in Entity Framework Core using eager loading. 我想使用急切加载在Entity Framework Core中获得多个嵌套级别的子表。 I don't think lazy loading is implemented yet. 我不认为延迟加载已经实现。

I found an answer for EF6. 我找到了EF6的答案

var company = context.Companies
                 .Include(co => co.Employees.Select(emp => emp.Employee_Car))
                 .Include(co => co.Employees.Select(emp => emp.Employee_Country))
                 .FirstOrDefault(co => co.companyID == companyID);

My problem is that Select is not recognized in EF Core 我的问题是在EF Core中无法识别Select

Error CS1061 'Employees' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Employees' could be found (are you missing a using directive or an assembly reference?) 错误CS1061“Employees”不包含“Select”的定义,也没有扩展方法“Select”接受类型为“Employees”的第一个参数(您是否缺少using指令或程序集引用?)

My included namespaces: 我包含的命名空间:

using MyProject.Models;
using Microsoft.Data.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

What is the alternative for Select in EF Core. 在EF Core中Select的替代方法是什么。

You can use the keyword ThenInclude instead 您可以使用关键字ThenInclude

eg 例如

var company = context.Companies
             .Include(co => co.Employees).ThenInclude(emp => emp.Employee_Car)
             .Include(co => co.Employees).ThenInclude(emp => emp.Employee_Country)
             .FirstOrDefault(co => co.companyID == companyID);

Also, the .ThenInclude intellisense for only works up to the 3rd level, for example: 此外,.ThenInclude intellisense仅适用于第3级,例如:

_Context.A.Include(a => a.B).ThenInclude(B => B.C).ThenInclude(C => C.D)

The last part of that statement: 该声明的最后部分:

 .ThenInclude(C => C.D)

won't show "D", so you have to type D in yourself, then wait for a short period of time for the compilation error to disappear! 不会显示“D”,所以你必须自己键入D,然后等待一小段时间让编译错误消失!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM