添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
英俊的豆芽  ·  Vue资源 | icode·  3 月前    · 
憨厚的充值卡  ·  Google ...·  7 月前    · 
有腹肌的香烟  ·  Python ...·  1 年前    · 

Entity Framework Classic Include

Description

The Include method lets you add related entities to the query result.

In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.

ctx.Customers .Include(customer => customer.Orders) .ThenInclude(order => order.OrderDetails) .ThenInclude(orderDetail => orderDetail.Product) .AlsoInclude(product => product.Category) .AlsoInclude(product => product.Supplier) .ToList();

Try it: NET Core | NET Framework

  • If you want to include items from the same level, use AlsoInclude
  • If you want to include items from the next level, use ThenInclude
  • Limitation

    DbQuery

    Chaining includes only work if the first include call is from a DbQuery . If you used some LINQ and the query is currently an IQueryable , you can use the method AsDbQuery to tell the compiler that's a DbQuery . This restriction is currently required to avoid some side impact with queries that are not directly using the DbQuery class.

    ctx.OrderDetails .Where(orderDetail => orderDetail.Quantity > 1 ) .AsDbQuery() .Include(orderDetail => orderDetail.Product) .AlsoInclude(product => product.Category) .AlsoInclude(product => product.Supplier) .ToList();

    Try it: NET Core | NET Framework

    It's planned to remove this limitation.