Database Reference
In-Depth Information
Figure 5-17. Accessing the Local collection never generates a SQL query
Next we add a new Club entity entitled the Lonesome Pine Club to the Local collection and, at the same time,
remove the Desert Sun Club from the Local collection. We then iterate through the context object for Clubs, which as
expected, generates a SQL query against the underlying data store, as shown in Figure 5-18 .
Figure 5-18. Querying the context object always generates a SQL query
Interestingly, in the context, we see that the Desert Sun Club has been marked for deletion, but we do not see the
newly added Lonesome Pine Club. Keep in mind that Lonesome Pine has been added to the Context object, but we
have not yet called the SaveChanges() operation to update the underlying data store.
However, when we iterate through the Local collection for Clubs, we do not generate a query to the underlying
data store, as shown in Figure 5-19 . Instead, we see the newly added Lonesome Pine Club, but we no longer see the
Desert Sun Club that is marked for deletion. The default behavior of the Local collection is to hide any entities that are
marked for deletion, as these objects are no longer valid.
Figure 5-19. Accessing the Local collection never generates a SQL query
The bottom line: Accessing the Local collection never causes a query to be sent to the database; accessing the
context object always causes a query to be sent to the database.
To summarize, each entity set exposes a property called Local, which is an observable collection that mirrors the
contents of the underlying context object. As demonstrated in this recipe, querying the Local Collection can be very
efficient in that doing so never generates a SQL query to the underlying data store.
5-5. Loading a Complete Object Graph
Problem
You have a model with several related entities, and you want to load the complete object graph of all the instances of
each entity in a single query. Normally, when a specific view requires a set of related entities in order to render, you'll
prefer this approach as opposed to the lazy loading approach that fetches related data with a number of smaller queries.
 
Search WWH ::




Custom Search