Database Reference
In-Depth Information
a separate query the first time either of the related tables are accessed. Once a query has been invoked for a property from
a related entity, Entity Framework will mark the property as loaded and will retrieve the data from memory as opposed to
requerying the underlying table over and over again. In this example, four separate queries are generated for child data:
A select statement against CustomerType and CustomerEmail for Joan Smith
A select statement against CustomerType and CustomerEmail for Bill Meyers
This separate query for each child table works well when a user is browsing your application and requests
different data elements depending on his or her needs at the moment. It can improve application response time,
since data is retrieved as needed with a series of small queries, as opposed to loading a large amount of data up front,
potentially causing a delay in rendering the view to the user.
This approach, however, is not so efficient when you know, up front, that you will require a large set of data from
related tables. In those cases, a query with eager loading may be a better option as it can retrieve all of the data
(from both the parent and related tables) in a single query.
The last code block, entitled 'Extra Credit,' demonstrates that once child properties are loaded, Entity Framework will
retrieve their values from in-memory and not requery the database. Turn on the SQL Server Profiler Tool, run the example
and note how the 'Extra Credit' code block does not generate SQL Select statements when child properties are referenced.
sQL server profiler is a great tool for inspecting the actual query statements generated by sQL server. it is free and
included with sQL server developer edition and better: http://technet.microsoft.com/en-us/library/ms181091.aspx
Note
5-2. Eager Loading Related Entities
Problem
You want to load an entity along with some related entities in a single trip to the database.
Solution
Let's say that you have a model like the one shown in Figure 5-2 .
Figure 5-2. A model with a Customer and its related information
Similar to Recipe 5-1, in this model we have a Customer entity with a single CustomerType and many
CustomerEmail addresses. The association with CustomerType is one-to-many with CustomerType on the one side of
the association. This is an entity reference .
 
 
Search WWH ::




Custom Search