Example 14 4 using a navigation property using var

Info icon This preview shows pages 580–583. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 14-4. Using a navigation property using (var dbContext = new AdventureWorksLT2008Entities()) { var customerOrderCounts = from cust in dbContext.Customers 556 | Chapter 14: Databases
Image of page 580

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
select new { cust.CustomerID, OrderCount = cust.SalesOrderHeaders.Count }; foreach (var customerInfo in customerOrderCounts) { Console.WriteLine("Customer {0} has {1} orders", customerInfo.CustomerID, customerInfo.OrderCount); } } The database table that the Customer entity class represents does not have a column called SalesOrderHeader . The Entity Framework wizard added this property to repre- sent the relationship between the Customer and SalesOrderHeader tables. This is not an ordinary property—in Figure 14-2 you can see that it appears separately, under Navi- gation Properties. From C# code, a navigation property looks like a collection. Example 14-4 just retrieves the Count property, but we could do more advanced things. The query in Exam- ple 14-5 has a nested query for each customer that looks for all shipped orders (those with a Status of 5 ), and for each one it reads the total due for that order and a count of all the SalesOrderDetails rows associated with that order. So this uses two navigation properties—the one representing the relationship between customers and orders, and the one representing the relationship between orders and order details. Example 14-5. Traversing multiple relationships with navigation properties var info = from cust in dbContext.Customers select new { cust.CustomerID, Orders = from order in cust.SalesOrderHeaders where order.Status == 5 select new { order.TotalDue, ItemCount = order.SalesOrderDetails.Count } }; There’s a reason we’ve used LINQ in these last two examples—it happens to avoid an issue with navigation properties. How does the EF decide how many entities to load for us, and when? In Example 14-4 , the LINQ query just retrieves two pieces of infor- mation for each customer—the CustomerID and the order count—and while Exam- ple 14-5 is more complex, it’s still circumscribed, so the EF can inspect the query to work out exactly what it needs to retrieve. But when we’re not using LINQ, how does the EF know what to do? For instance, consider the code in Example 14-6 . The Entity Data Model | 557
Image of page 581
Example 14-6. Following an association after the initial query Customer myCustomer = dbContext.Customers.Single( cust => cust.CustomerID == 29531); Console.WriteLine(myCustomer.SalesOrderHeaders.Count); This fetches the entity for a specific customer, and then tries to get the number of SalesOrderHeader entities to which this item is related. Prior to .NET 4, this did not work—it would print out 0 , even though the example database has one related order for this customer. In .NET 3.5 SP1, the Entity Framework would initialize navigation properties such as the Customer object’s SalesOrderHeaders property with an empty collection, and would load the related objects only if we ask it to, using the Load method shown in Example 14-7 .
Image of page 582

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 583
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern