Database Reference
In-Depth Information
To fetch the parent customer entity objects and all of the related CustomerEmail entities and CustomerType
entity objects at once, we use the Include() method syntax, as shown in Listing 5-2.
Listing 5-2. Eager Loading of Instances of Customertype and Customeremail Along with Instances of Customer
using (var context = new EFRecipesEntities())
{
var web = new CustomerType { Description = "Web Customer",
CustomerTypeId = 1 };
var retail = new CustomerType { Description = "Retail Customer",
CustomerTypeId = 2 };
var customer = new Customer { Name = "Joan Smith", CustomerType = web };
customer.CustomerEmails.Add(new CustomerEmail
{ Email = " jsmith@gmail.com " });
customer.CustomerEmails.Add(new CustomerEmail { Email = " joan@smith.com " });
context.Customers.Add(customer);
customer = new Customer { Name = "Bill Meyers", CustomerType = retail };
customer.CustomerEmails.Add(new CustomerEmail
{ Email = " bmeyers@gmail.com " });
context.Customers.Add(customer);
context.SaveChanges();
}
using (var context = new EFRecipesEntities())
{
// Include() method with a string-based query path to the
// corresponding navigation properties
var customers = context.Customers
.Include("CustomerType")
.Include("CustomerEmails");
Console.WriteLine("Customers");
Console.WriteLine("=========");
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)
{
Console.WriteLine("\t{0}", email.Email);
}
}
}
using (var context = new EFRecipesEntities())
{
// Include() method with a strongly typed query path to the
// corresponding navigation properties
var customerTypes = context.CustomerTypes
.Include(x => x.Customers)
.Select(y =>y.CustomerEmails));
 
Search WWH ::




Custom Search