加载具有IEnumerable <t>属性的实体,以及IEnumerable属性的每个元素内的另一个对象

时间:2019-01-10 08:59:34

标签: entity-framework entity-framework-6

使用Entity Framework 6(尤其是)加载实体时,包含两级嵌套属性的语法是什么。中级是可以枚举的?

请考虑以下示例。

class Foo
{
  ..

  public virtual ICollection<Bar> Bars { get; set; }
}

class Bar
{
  ...

  public Gaz Gaz { get; set; }
}


class Repo
{
 public async Task<IEnumerable<Foo>> GetFoosAsync()
 {
  var foos = await context
    .Foos
    .Include(foo => foo.Bars)
    /* .Include("Bars/Gaz") // here I would like to include 
        the Gaz's in each Bar as well. Is this the right syntax? 
        Is there also a Expression<Func<>> syntax so I can strongly type 
        this? Because I can't do Include(foo => foo.Bars.Gaz) because 
        Bars is an IEnumerable<T> */
    .ToListAsync();

   return foos;
 }
}

我正在使用面向.NET Framework 4.6.1的Entity Framework 6.2.0。

0 个答案:

没有答案