Home Forums SQL Server 2005 T-SQL (SS2K5) Pls help Understanding Entity Framework generated Query vs Custom Query RE: Pls help Understanding Entity Framework generated Query vs Custom Query

  • Grant,

    EF does JOINs exactly like we do in T-SQL.

    Example: There are two tables in AdventureWorks database, Employee and Contact. Each Employee can have more than one contact but Contact's cannot have more than one employee!!.

    In order to Load it you can request EF to do it in two different approach.

    Approach One: "Query Path", You request EF to load all the related data at the time you load your parent. When you do this EF will issue a LEFT JOIN and load all the related tables: See Following query:

    SELECT

    1 AS [C1],

    [Extent1].[EmployeeID] AS [EmployeeID],

    [Extent1].[DepartmentID] AS [DepartmentID],

    [Extent1].[ShiftID] AS [ShiftID],

    [Extent1].[StartDate] AS [StartDate],

    [Extent1].[EndDate] AS [EndDate],

    [Extent1].[ModifiedDate] AS [ModifiedDate],

    1 AS [C2],

    [Extent2].[EmployeeID] AS [EmployeeID1],

    [Extent2].[NationalIDNumber] AS [NationalIDNumber],

    [Extent2].[LoginID] AS [LoginID],

    [Extent2].[Title] AS [Title],

    [Extent2].[BirthDate] AS [BirthDate],

    [Extent2].[MaritalStatus] AS [MaritalStatus],

    [Extent2].[Gender] AS [Gender],

    [Extent2].[HireDate] AS [HireDate],

    [Extent2].[SalariedFlag] AS [SalariedFlag],

    [Extent2].[VacationHours] AS [VacationHours],

    [Extent2].[SickLeaveHours] AS [SickLeaveHours],

    [Extent2].[CurrentFlag] AS [CurrentFlag],

    [Extent2].[rowguid] AS [rowguid],

    [Extent2].[ModifiedDate] AS [ModifiedDate1],

    [Extent3].[ContactID] AS [ContactID],

    [Extent3].[NameStyle] AS [NameStyle],

    [Extent3].[Title] AS [Title1],

    [Extent3].[FirstName] AS [FirstName],

    [Extent3].[MiddleName] AS [MiddleName],

    [Extent3].[LastName] AS [LastName],

    [Extent3].[Suffix] AS [Suffix],

    [Extent3].[EmailAddress] AS [EmailAddress],

    [Extent3].[EmailPromotion] AS [EmailPromotion],

    [Extent3].[Phone] AS [Phone],

    [Extent3].[PasswordHash] AS [PasswordHash],

    [Extent3].[PasswordSalt] AS [PasswordSalt],

    [Extent3].[AdditionalContactInfo] AS [AdditionalContactInfo],

    [Extent3].[rowguid] AS [rowguid1],

    [Extent3].[ModifiedDate] AS [ModifiedDate2],

    [Extent2].[ManagerID] AS [ManagerID]

    FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]

    LEFT OUTER JOIN [HumanResources].[Employee] AS [Extent2]

    ON [Extent1].[EmployeeID] = [Extent2].[EmployeeID]

    LEFT OUTER JOIN [Person].[Contact] AS [Extent3]

    ON [Extent2].[ContactID] = [Extent3].[ContactID]

    Approach 2: "Explicit Load"

    You load the Parent first and whenever you want you load the child. In this case EF will issue one D-SQL to DB and get the Parent rows. After that when you want the related child rows you load the child for one parent at a time, which mean you making multiple trips to DB but same D-SQL with different Parent ID. The queries look like this: This works only in SQL 2005.

    -- SQL Generated from EF

    SELECT

    1 AS [C1],

    [Extent1].[EmployeeID] AS [EmployeeID],

    [Extent1].[NationalIDNumber] AS [NationalIDNumber],

    [Extent1].[LoginID] AS [LoginID],

    [Extent1].[Title] AS [Title],

    [Extent1].[BirthDate] AS [BirthDate],

    [Extent1].[MaritalStatus] AS [MaritalStatus],

    [Extent1].[Gender] AS [Gender],

    [Extent1].[HireDate] AS [HireDate],

    [Extent1].[SalariedFlag] AS [SalariedFlag],

    [Extent1].[VacationHours] AS [VacationHours],

    [Extent1].[SickLeaveHours] AS [SickLeaveHours],

    [Extent1].[CurrentFlag] AS [CurrentFlag],

    [Extent1].[rowguid] AS [rowguid],

    [Extent1].[ModifiedDate] AS [ModifiedDate],

    [Extent1].[ContactID] AS [ContactID],

    [Extent1].[ManagerID] AS [ManagerID]

    FROM [HumanResources].[Employee] AS [Extent1]

    -- SQL Generated from EF

    exec sp_executesql N'SELECT

    1 AS [C1],

    [Extent1].[EmployeeID] AS [EmployeeID],

    [Extent1].[DepartmentID] AS [DepartmentID],

    [Extent1].[ShiftID] AS [ShiftID],

    [Extent1].[StartDate] AS [StartDate],

    [Extent1].[EndDate] AS [EndDate],

    [Extent1].[ModifiedDate] AS [ModifiedDate]

    FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]

    WHERE [Extent1].[EmployeeID] = @EntityKeyValue1',

    N'@EntityKeyValue1 int',@EntityKeyValue1=1

    exec sp_executesql N'SELECT

    1 AS [C1],

    [Extent1].[EmployeeID] AS [EmployeeID],

    [Extent1].[DepartmentID] AS [DepartmentID],

    [Extent1].[ShiftID] AS [ShiftID],

    [Extent1].[StartDate] AS [StartDate],

    [Extent1].[EndDate] AS [EndDate],

    [Extent1].[ModifiedDate] AS [ModifiedDate]

    FROM [HumanResources].[EmployeeDepartmentHistory] AS [Extent1]

    WHERE [Extent1].[EmployeeID] = @EntityKeyValue1',

    N'@EntityKeyValue1 int',@EntityKeyValue1=285

    HTH,

    Nachi