簡體   English   中英

如何將 Collection 中的屬性包含在 EntityFramework 核心中

[英]How to include a property from Collection with EntityFramework core

我有這樣的關系:

public class Company
{
  public virtual ICollection<Employee> Employee { get; set; }
}

在員工內部,我有一個 Profile 屬性:

public class Employee
{
    public virtual Profile Profile { get; set; }
}

我能夠獲得所有員工,但不能獲得Profile屬性。

和請求:

var result = await Context.Company.Include(a => a.Employee).Where(a => a.Token == Token).SingleOrDefaultAsync();

結果被檢索,但不是配置文件。

正如@jcruz 指出的,EF Core 中的 ThenInclude 是您想要的:

var result = await Context.Company.Include(a => a.Employee).ThenInclude(e => e.Profile).Where(a => a.Token == Token).SingleOrDefaultAsync();

Include() function 更改為字符串重載並包含Profile屬性。

var result = await Context.Company.Include("Employee.Profile").Where(a => a.Token == Token).SingleOrDefaultAsync();

除了 ThenInclude,您還可以使用 join 編寫查詢語法。 當然你應該先得到你的清單; 那將是昂貴的。 https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-inner-joins

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM