score:220

Accepted answer

let's say for instance you want to get a list of all your customers:

var customers = context.customers.tolist();

and let's assume that each customer object has a reference to its set of orders, and that each order has references to lineitems which may also reference a product.

as you can see, selecting a top-level object with many related entities could result in a query that needs to pull in data from many sources. as a performance measure, include() allows you to indicate which related entities should be read from the database as part of the same query.

using the same example, this might bring in all of the related order headers, but none of the other records:

var customerswithorderdetail = context.customers.include("orders").tolist();

as a final point since you asked for sql, the first statement without include() could generate a simple statement:

select * from customers;

the final statement which calls include("orders") may look like this:

select *
from customers join orders on customers.id = orders.customerid;

score:2

think of it as enforcing eager-loading in a scenario where your sub-items would otherwise be lazy-loading.

the query ef is sending to the database will yield a larger result at first, but on access no follow-up queries will be made when accessing the included items.

on the other hand, without it, ef would execute separte queries later, when you first access the sub-items.

score:31

i just wanted to add that "include" is part of eager loading. it is described in entity framework 6 tutorial by microsoft. here is the link: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application


excerpt from the linked page:

here are several ways that the entity framework can load related data into the navigation properties of an entity:

lazy loading. when the entity is first read, related data isn't retrieved. however, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. this results in multiple queries sent to the database — one for the entity itself and one each time that related data for the entity must be retrieved. the dbcontext class enables lazy loading by default.

eager loading. when the entity is read, related data is retrieved along with it. this typically results in a single join query that retrieves all of the data that's needed. you specify eager loading by using the include method.

explicit loading. this is similar to lazy loading, except that you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. you load related data manually by getting the object state manager entry for an entity and calling the collection.load method for collections or the reference.load method for properties that hold a single entity. (in the following example, if you wanted to load the administrator navigation property, you'd replace collection(x => x.courses) with reference(x => x.administrator).) typically you'd use explicit loading only when you've turned lazy loading off.

because they don't immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.


Related Query