Entity Framework Classic AlsoInclude
You can chain multiple related objects to the query result by using the
ThenInclude methods. The
AlsoInclude method doesn't move the chaining level. It allows to include multiple related objects from the same level.
AlsoInclude is a syntactic sugar method to make it easier and clearer to include multiple related objects.
ctx.OrderDetails .Include(orderDetail => orderDetail.Product) .AlsoInclude(product => product.Category) .AlsoInclude(product => product.Supplier) .ToList();
- If you want to reset the level to the root, use
- If you want to include items from the next level, use
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
This restriction is currently required to avoid some side impact with queries that are not directly using
ctx.OrderDetails .Where(orderDetail => orderDetail.Quantity > 1) .AsDbQuery() .Include(orderDetail => orderDetail.Product) .AlsoInclude(product => product.Category) .AlsoInclude(product => product.Supplier) .ToList();