Database Reference
In-Depth Information
CustomerEmail entity for the customer. By chaining together the Include() method twice in a fluent API manner,
we fetch referenced entities from both of the Customer's navigation properties. Note that in this example we use string
representations of the navigation properties, separated by the “ . ” character, to identify the related entity objects.
The string representation is referred as the query path of the related objects.
In the following foreach construct, we perform the exact same operation, but using strongly typed query paths.
Note here how we use lambda expressions to identify each of the related entities. The strongly typed usage provides us
with both IntelliSense, compile-time safety and refactoring support.
Note that the SQL query that is generated in Listing 5-3 is generated from usage of the Include() method. Entity
Framework automatically removes data that is duplicated by the query, as shown in Figure 5-3 , before the result is
materialized and sent back to the application.
Listing 5-3. The SQL Query Resulting from Our Use of the Include() Method
SELECT
[Project1].[CustomerId] AS [CustomerId],
[Project1].[Name] AS [Name],
[Project1].[CustomerTypeId] AS [CustomerTypeId],
[Project1].[CustomerTypeId1] AS [CustomerTypeId1],
[Project1].[Description] AS [Description],
[Project1].[C1] AS [C1],
[Project1].[CustomerEmailId] AS [CustomerEmailId],
[Project1].[CustomerId1] AS [CustomerId1],
[Project1].[Email] AS [Email]
FROM ( SELECT
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[Name] AS [Name],
[Extent1].[CustomerTypeId] AS [CustomerTypeId],
[Extent2].[CustomerTypeId] AS [CustomerTypeId1],
[Extent2].[Description] AS [Description],
[Extent3].[CustomerEmailId] AS [CustomerEmailId],
[Extent3].[CustomerId] AS [CustomerId1],
[Extent3].[Email] AS [Email],
CASE WHEN ([Extent3].[CustomerEmailId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [Chapter5].[Customer] AS [Extent1]
INNER JOIN [Chapter5].[CustomerType] AS [Extent2] ON
[Extent1].[CustomerTypeId] = [Extent2].[CustomerTypeId]
LEFT OUTER JOIN [Chapter5].[CustomerEmail] AS [Extent3] ON
[Extent1].[CustomerId] = [Extent3].[CustomerId]
) AS [Project1]
ORDER BY [Project1].[CustomerId] ASC, [Project1].[CustomerTypeId1] ASC, [Project1].[C1] ASC
Figure 5-3. Redundant data resulting from the Include() method
 
Search WWH ::




Custom Search