Database Reference
In-Depth Information
Figure 5-1. A model with a Customer and its related information
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 .
The association with CustomerEmail is also one-to-many but with CustomerEmail on the many side of the
association. This is an entity collection .
When we put all three entity classes together, we arrive at a construct called an object graph . An object graph is
a set of individual, but related entities, which together form a logical whole unit . Specifically, an object graph is a view
of a given entity and its related entities at a specific point in time. For example, during an operation in our application,
a customer with an Id of 5 may contain the name “John Smith,” have a customer type of “preferred,” and a collection
of 10 customer emails.
Listing 5-1 demonstrates the lazy loading behavior of Entity Framework, which is the default behavior for loading
related entity objects.
Listing 5-1. Lazy Loading of Instances of Customertype and Customeremail Along with Instances of Customer
using (var context = new EFRecipesEntities())
{
var customers = context.Customers;
Console.WriteLine("Customers");
Console.WriteLine("=========");
// Only information from the Customer entity is requested
foreach (var customer in customers)
{
Console.WriteLine("Customer name is {0}", customer.Name);
}
// Now, application is requesting information from the related entities, CustomerType
// and CustomerEmail, resulting in Entity Framework generating separate queries to each
// entity object in order to obtain the requested information.
foreach (var customer in customers)
{
Console.WriteLine("{0} is a {1}, email address(es)", customer.Name,
customer.CustomerType.Description);
foreach (var email in customer.CustomerEmails)
 
Search WWH ::




Custom Search