Database Reference
In-Depth Information
How It Works
A query path is a string or strongly typed argument that is passed to the Include() method. A query path represents
the entire path of the object graph that you want to load with the Include() method. The Include() method extends
the query to include the entities referenced along the query path.
In Listing 5-12, we start by demonstrating the Include() method with string-based query parameters. Include()
is invoked first with a query path parameter that includes the part of the graph extending through Section to
Instructor. This modifies the query to include all of the Sections and their Instructors. Then, chained to the first
Include() method is another Include() method that includes a path extending through Section to Student. This
modifies the query to include Sections and their Students. The result is a materialization of the complete object graph
including all Course entities along with entities on each end of the associations in the model.
In the second part of Listing 5-12, we demonstrate the usage of the Include() method with strongly typed query
path parameters. Notice how both Include() methods here combine one parameter, Sections, with the associated
Instructor and Student entity objects by using a Select() method.
the overloaded Include() method that accepts strongly-typed parameters is an extension method that is
exposed from the System.Data.Entity namespace. You will need to add a using directive to your class that references
this namespace in order to use the overloaded version of this method.
Note
You can construct query paths from navigation properties to any depth. This gives you a great deal of flexibility
in partial or complete object graph loading. Entity Framework attempts to optimize the final query generation by
pruning off overlapping or duplicate query paths.
The syntax and semantics of the Include() method are deceptively simple. Don't let the simplicity fool you
into thinking that there is no performance price to be paid when using the Include() method. Eager loading with
several Include() method invocations can rapidly increase the complexity of the query sent to the database and
dramatically increase the amount of data returned from the database. The complex queries generated can lead to
poor performance plan generation, and the large amount of returned data can cause Entity Framework to spend an
inordinate amount of time removing duplicate data. You would be wise to profile all queries generated from usage of
the Include() method to ensure that you are not causing potential performance problems for your application.
5-6. Loading Navigation Properties on Derived Types
Problem
You have a model with one or more derived types that are in a Has-a relationship (wherein one object is a part of
another object) with one or more other entities. You want to eagerly load all of the related entities in one round trip
to the database.
Solution
Suppose that you have a model like the one in Figure 5-21 .
 
 
Search WWH ::




Custom Search