简体   繁体   English

如何将 Collection 中的属性包含在 EntityFramework 核心中

[英]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.

相关问题 在ASP.NET EntityFramework Core中包含通用列表属性的实体 - Include Entity of generic list property in ASP.NET EntityFramework Core 是否可以暗示 EntityFramework Core 使用内部连接而不是 EXISTS 子查询来获取导航属性集合条件? - Is it possible to hint at EntityFramework Core to use an inner join instead of an EXISTS sub-query for navigation property collection conditions? 如何从 EntityFramework Core 中的 DataContext 运行原始 SQL 查询 - How to run a raw SQL query from DataContext in EntityFramework Core 如何从entityframework核心中的多对多关系中获取字段列表? - How to get a list of fields from many to many relationships in entityframework core? EntityFramework Core Fluent模型生成器键和属性 - EntityFramework Core Fluent Model Builder Key and Property EntityFramework Core自动生成密钥id属性 - EntityFramework Core auto generate key id property 实体框架核心-多对多-属性表达式无效 - entityframework core - many to many - The property expression is not valid 如何在EF核心中包含虚拟ICollection以及该集合中的特定项目? - How to include virtual ICollection and also specific item from this collection in EF core? 如何在EntityFramework和ESQL中基于子级属性从父级中进行选择 - How Select From Parent Based on Child Property in EntityFramework and ESQL C#Entityframework-如何将导航属性从HashSet转换为SortedSet - C# Entityframework - How to convert navigation property from HashSet to SortedSet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM