[英]How to include a property from Collection with EntityFramework core
I have this relationship:我有这样的关系:
public class Company
{
public virtual ICollection<Employee> Employee { get; set; }
}
Inside of Employee I have a Profile property:在员工内部,我有一个 Profile 属性:
public class Employee
{
public virtual Profile Profile { get; set; }
}
I'm able to get all the employees but not the Profile
property.我能够获得所有员工,但不能获得Profile
属性。
And the request:和请求:
var result = await Context.Company.Include(a => a.Employee).Where(a => a.Token == Token).SingleOrDefaultAsync();
The result is retrieved but not the Profile.结果被检索,但不是配置文件。
as @jcruz pointed out ThenInclude in EF Core is what you want:正如@jcruz 指出的,EF Core 中的 ThenInclude 是您想要的:
var result = await Context.Company.Include(a => a.Employee).ThenInclude(e => e.Profile).Where(a => a.Token == Token).SingleOrDefaultAsync();
Change the Include()
function to the string overload and include the Profile
property.将Include()
function 更改为字符串重载并包含Profile
属性。
var result = await Context.Company.Include("Employee.Profile").Where(a => a.Token == Token).SingleOrDefaultAsync();
In addition to ThenInclude, you can write query syntax with join.除了 ThenInclude,您还可以使用 join 编写查询语法。 Sure you should get your lists first;当然你应该先得到你的清单; that will be costly.那将是昂贵的。 https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.