LazyLoading with EF和存储过程

时间:2016-04-19 10:44:05

标签: c# entity-framework

我的EF需要花费很长时间才能执行,并且在使用SQL Server Profiler时,我可以看到它将我的查询分成几个部分,这些部分都在加起来。

我决定创建一个存储过程来减少加载时间。它看起来像这样:

CREATE PROCEDURE GetAllProducts
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        a.*, 
        b.*, 
        b1.*,
        c.*, 
        preview.*,
        e.*, 
        a1.*, 
        a2.*,
        a3.*,
        a4.*,
        a5.*,
        a6.*,
        a7.*,
        a8.*,
        a9.*,
        a10.*,
        a11.*,
        a12.*,
        a13.*
    FROM 
        Products AS a
    INNER JOIN 
        Details AS b ON a.DetailId = b.Id
    INNER JOIN 
        Categories AS c ON a.CategoryId = c.Id
    INNER JOIN 
        Previews AS preview ON a.PreviewId = preview.Id
    INNER JOIN 
        CameraAttributes AS e ON a.AttributeId = e.Id
    INNER JOIN 
        CameraAttributePicture AS a1 ON e.Id = a1.AttributeId
    INNER JOIN 
        CameraAttributeVideo AS a2 ON e.Id = a2.AttributeId
    INNER JOIN 
        CameraAttributeAudio AS a3 ON e.Id = a3.AttributeId
    INNER JOIN 
        CameraAttributeBattery AS a4 ON e.Id = a4.AttributeId
    INNER JOIN 
        CameraAttributeDimensions AS a5 ON e.Id = a5.AttributeId
    INNER JOIN 
        CameraAttributeDisplay AS a6 ON e.Id = a6.AttributeId
    INNER JOIN 
        CameraAttributeLightExposure AS a7 ON e.Id = a7.AttributeId 
    INNER JOIN 
        CameraAttributeFlash as a8 ON e.Id = a8.AttributeId
    INNER JOIN 
        CameraAttributeFocusing AS a9 ON e.Id = a9.AttributeId 
    INNER JOIN 
        CameraAttributeInterface AS a10 ON e.Id = a10.AttributeId
    INNER JOIN 
        CameraAttributeLens AS a11 ON e.Id = a11.AttributeId
    INNER JOIN 
        CameraAttributeLensZoom AS b1 ON a11.id = b1.LensId
    INNER JOIN 
        CameraAttributeNetwork AS a12 ON e.Id = a12.AttributeId
    INNER JOIN 
        CameraAttributeShutter AS a13 ON e.Id = a13.AttributeId
END
GO

我已经加入了查询中的所有表,希望它们能够在我的应用程序中匹配我的模型:

public class Product
{
    public int Id { get; set; }
    [MaxLength(255)] public string GTIN { get; set; }
    [Required] [MaxLength(100)] public string ProductId { get; set; }
    [Required] [MaxLength(100)] public string Make { get; set; }
    [MaxLength(100)] public string Model { get; set; }
    public bool Expert { get; set; }
    public bool Sponsored { get; set; }

    public virtual CameraAttribute Attributes { get; set; }
    public virtual Detail Details { get; set; }
    public virtual Category Category { get; set; }
    public virtual Preview Preview { get; set; }
}

问题是,所有虚拟属性都为空。

我假设在使用存储过程时我无法延迟加载它们,那么如何才能填充属性?

我无法发布所有内容,因为它会导致浏览器崩溃,但这是一个片段。我希望有所帮助:

Opened connection asynchronously at 19/04/2016 12:31:57 +01:00

'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-38-131055390555737826): Loaded 'EntityFrameworkDynamicProxies-Piiick.Web'. 
SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[GTIN] AS [GTIN], 
    [Extent1].[ProductId] AS [ProductId], 
    [Extent1].[Make] AS [Make], 
    [Extent1].[Model] AS [Model], 
    [Extent1].[Expert] AS [Expert], 
    [Extent1].[Sponsored] AS [Sponsored], 
    [Extent1].[AttributeId] AS [AttributeId], 
    [Extent1].[CategoryId] AS [CategoryId], 
    [Extent1].[DetailId] AS [DetailId], 
    [Extent1].[PreviewId] AS [PreviewId]
    FROM [dbo].[Products] AS [Extent1]


-- Executing asynchronously at 19/04/2016 12:31:57 +01:00

-- Completed in 7 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:31:57 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Join13].[Id1] AS [Id], 
    [Join13].[CompatibleMemory] AS [CompatibleMemory], 
    [Join13].[WhiteBalance] AS [WhiteBalance], 
    [Join13].[SceneModes] AS [SceneModes], 
    [Join13].[ShootingModes] AS [ShootingModes], 
    [Join13].[PhotoEffects] AS [PhotoEffects], 
    [Join13].[CameraPlayback] AS [CameraPlayback], 
    [Join13].[Tripod] AS [Tripod], 
    [Join13].[DirectPrinting] AS [DirectPrinting], 
    [Join13].[Colour] AS [Colour], 
    [Join13].[Id2] AS [Id1], 
    [Join13].[Id3] AS [Id2], 
    [Join13].[Id4] AS [Id3], 
    [Join13].[Id5] AS [Id4], 
    [Join13].[Id6] AS [Id5], 
    [Join13].[Id7] AS [Id6], 
    [Join13].[Id8] AS [Id7], 
    [Join13].[Id9] AS [Id8], 
    [Join13].[Id10] AS [Id9], 
    [Join13].[Id11] AS [Id10], 
    [Join13].[Id12] AS [Id11], 
    [Join13].[Id13] AS [Id12], 
    [Join13].[Id14] AS [Id13]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN  (SELECT [Extent2].[Id] AS [Id1], [Extent2].[CompatibleMemory] AS [CompatibleMemory], [Extent2].[WhiteBalance] AS [WhiteBalance], [Extent2].[SceneModes] AS [SceneModes], [Extent2].[ShootingModes] AS [ShootingModes], [Extent2].[PhotoEffects] AS [PhotoEffects], [Extent2].[CameraPlayback] AS [CameraPlayback], [Extent2].[Tripod] AS [Tripod], [Extent2].[DirectPrinting] AS [DirectPrinting], [Extent2].[Colour] AS [Colour], [Extent3].[Id] AS [Id2], [Extent4].[Id] AS [Id3], [Extent5].[Id] AS [Id4], [Extent6].[Id] AS [Id5], [Extent7].[Id] AS [Id6], [Extent8].[Id] AS [Id7], [Extent9].[Id] AS [Id8], [Extent10].[Id] AS [Id9], [Extent11].[Id] AS [Id10], [Extent12].[Id] AS [Id11], [Extent13].[Id] AS [Id12], [Extent14].[Id] AS [Id13], [Extent15].[Id] AS [Id14]
        FROM              [dbo].[CameraAttributes] AS [Extent2]
        LEFT OUTER JOIN [dbo].[CameraAttributeAudio] AS [Extent3] ON [Extent2].[Id] = [Extent3].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeBattery] AS [Extent4] ON [Extent2].[Id] = [Extent4].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeDimensions] AS [Extent5] ON [Extent2].[Id] = [Extent5].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeDisplay] AS [Extent6] ON [Extent2].[Id] = [Extent6].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeLightExposure] AS [Extent7] ON [Extent2].[Id] = [Extent7].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeFlash] AS [Extent8] ON [Extent2].[Id] = [Extent8].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeFocusing] AS [Extent9] ON [Extent2].[Id] = [Extent9].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeInterface] AS [Extent10] ON [Extent2].[Id] = [Extent10].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeLens] AS [Extent11] ON [Extent2].[Id] = [Extent11].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeNetwork] AS [Extent12] ON [Extent2].[Id] = [Extent12].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributePicture] AS [Extent13] ON [Extent2].[Id] = [Extent13].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeShutter] AS [Extent14] ON [Extent2].[Id] = [Extent14].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeVideo] AS [Extent15] ON [Extent2].[Id] = [Extent15].[AttributeId] ) AS [Join13] ON [Extent1].[AttributeId] = [Join13].[Id1]
    WHERE [Extent1].[Id] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 11 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[SupportedFormats] AS [SupportedFormats], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeAudio] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[PowerSource] AS [PowerSource], 
    [Extent1].[Technology] AS [Technology], 
    [Extent1].[Life] AS [Life], 
    [Extent1].[Type] AS [Type], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeBattery] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 6 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Width] AS [Width], 
    [Extent1].[Depth] AS [Depth], 
    [Extent1].[Height] AS [Height], 
    [Extent1].[Weight] AS [Weight], 
    [Extent1].[WeightIncludingBattery] AS [WeightIncludingBattery], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeDimensions] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 3 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Type] AS [Type], 
    [Extent1].[Diagonal] AS [Diagonal], 
    [Extent1].[Resolution] AS [Resolution], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeDisplay] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[IsoSensitivity] AS [IsoSensitivity], 
    [Extent1].[Mode] AS [Mode], 
    [Extent1].[Correction] AS [Correction], 
    [Extent1].[Metering] AS [Metering], 
    [Extent1].[Minimum] AS [Minimum], 
    [Extent1].[Maxiumum] AS [Maxiumum], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeLightExposure] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Modes] AS [Modes], 
    [Extent1].[ExposureLock] AS [ExposureLock], 
    [Extent1].[RangeWide] AS [RangeWide], 
    [Extent1].[RangeTelephoto] AS [RangeTelephoto], 
    [Extent1].[RechargeTime] AS [RechargeTime], 
    [Extent1].[Speed] AS [Speed], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeFlash] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 2 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Focus] AS [Focus], 
    [Extent1].[Adjustment] AS [Adjustment], 
    [Extent1].[AutoFocusModes] AS [AutoFocusModes], 
    [Extent1].[ClosestDistance] AS [ClosestDistance], 
    [Extent1].[NormalRange] AS [NormalRange], 
    [Extent1].[MacroRangeTelephoto] AS [MacroRangeTelephoto], 
    [Extent1].[MacroRangeWide] AS [MacroRangeWide], 
    [Extent1].[AutoModeTelephoto] AS [AutoModeTelephoto], 
    [Extent1].[AutoModeWide] AS [AutoModeWide], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeFocusing] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[PictBridge] AS [PictBridge], 
    [Extent1].[USBVersion] AS [USBVersion], 
    [Extent1].[USBType] AS [USBType], 
    [Extent1].[HDMI] AS [HDMI], 
    [Extent1].[HDMIType] AS [HDMIType], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeInterface] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Join1].[Id1] AS [Id], 
    [Join1].[FocalLength] AS [FocalLength], 
    [Join1].[MinimumFocalLength] AS [MinimumFocalLength], 
    [Join1].[MaximumFocalLength] AS [MaximumFocalLength], 
    [Join1].[MinimumAperture] AS [MinimumAperture], 
    [Join1].[MaximumAperture] AS [MaximumAperture], 
    [Join1].[LensStructure] AS [LensStructure], 
    [Join1].[Id2] AS [Id1], 
    [Join1].[AttributeId] AS [AttributeId]
    FROM  [dbo].[CameraAttributeLens] AS [Extent1]
    INNER JOIN  (SELECT [Extent2].[Id] AS [Id1], [Extent2].[FocalLength] AS [FocalLength], [Extent2].[MinimumFocalLength] AS [MinimumFocalLength], [Extent2].[MaximumFocalLength] AS [MaximumFocalLength], [Extent2].[MinimumAperture] AS [MinimumAperture], [Extent2].[MaximumAperture] AS [MaximumAperture], [Extent2].[LensStructure] AS [LensStructure], [Extent2].[AttributeId] AS [AttributeId], [Extent3].[Id] AS [Id2]
        FROM  [dbo].[CameraAttributeLens] AS [Extent2]
        LEFT OUTER JOIN [dbo].[CameraAttributeLensZoom] AS [Extent3] ON [Extent2].[Id] = [Extent3].[LensId] ) AS [Join1] ON [Extent1].[Id] = [Join1].[Id1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 2 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Optical] AS [Optical], 
    [Extent1].[Digital] AS [Digital], 
    [Extent1].[ExtraSmart] AS [ExtraSmart], 
    [Extent1].[Combined] AS [Combined], 
    [Extent1].[LensId] AS [LensId]
    FROM [dbo].[CameraAttributeLensZoom] AS [Extent1]
    WHERE [Extent1].[LensId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[WiFi] AS [WiFi], 
    [Extent1].[WiFiStandards] AS [WiFiStandards], 
    [Extent1].[NFC] AS [NFC], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeNetwork] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 2 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Megapixel] AS [Megapixel], 
    [Extent1].[Type] AS [Type], 
    [Extent1].[SensorType] AS [SensorType], 
    [Extent1].[MaxResolution] AS [MaxResolution], 
    [Extent1].[Resolutions] AS [Resolutions], 
    [Extent1].[Stablizer] AS [Stablizer], 
    [Extent1].[Location] AS [Location], 
    [Extent1].[SupportedAspectRatios] AS [SupportedAspectRatios], 
    [Extent1].[TotalMegapixels] AS [TotalMegapixels], 
    [Extent1].[Formats] AS [Formats], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributePicture] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 2 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[FastestSpeed] AS [FastestSpeed], 
    [Extent1].[SlowestSpeed] AS [SlowestSpeed], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeShutter] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[MaxResolution] AS [MaxResolution], 
    [Extent1].[Resolutions] AS [Resolutions], 
    [Extent1].[CaptureResolution] AS [CaptureResolution], 
    [Extent1].[FrameRate] AS [FrameRate], 
    [Extent1].[FullHD] AS [FullHD], 
    [Extent1].[SupportedFormats] AS [SupportedFormats], 
    [Extent1].[AttributeId] AS [AttributeId]
    FROM [dbo].[CameraAttributeVideo] AS [Extent1]
    WHERE [Extent1].[AttributeId] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent2].[Id] AS [Id], 
    [Extent2].[Name] AS [Name]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryId] = [Extent2].[Id]
    WHERE [Extent1].[Id] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Extent2].[Id] AS [Id], 
    [Extent2].[Title] AS [Title], 
    [Extent2].[Description] AS [Description], 
    [Extent2].[ShortDescription] AS [ShortDescription], 
    [Extent2].[Summary] AS [Summary], 
    [Extent2].[ShortSummary] AS [ShortSummary]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN [dbo].[Details] AS [Extent2] ON [Extent1].[DetailId] = [Extent2].[Id]
    WHERE [Extent1].[Id] = @EntityKeyValue1


-- EntityKeyValue1: '1' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 1 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

Opened connection at 19/04/2016 12:32:01 +01:00

SELECT 
    [Join13].[Id1] AS [Id], 
    [Join13].[CompatibleMemory] AS [CompatibleMemory], 
    [Join13].[WhiteBalance] AS [WhiteBalance], 
    [Join13].[SceneModes] AS [SceneModes], 
    [Join13].[ShootingModes] AS [ShootingModes], 
    [Join13].[PhotoEffects] AS [PhotoEffects], 
    [Join13].[CameraPlayback] AS [CameraPlayback], 
    [Join13].[Tripod] AS [Tripod], 
    [Join13].[DirectPrinting] AS [DirectPrinting], 
    [Join13].[Colour] AS [Colour], 
    [Join13].[Id2] AS [Id1], 
    [Join13].[Id3] AS [Id2], 
    [Join13].[Id4] AS [Id3], 
    [Join13].[Id5] AS [Id4], 
    [Join13].[Id6] AS [Id5], 
    [Join13].[Id7] AS [Id6], 
    [Join13].[Id8] AS [Id7], 
    [Join13].[Id9] AS [Id8], 
    [Join13].[Id10] AS [Id9], 
    [Join13].[Id11] AS [Id10], 
    [Join13].[Id12] AS [Id11], 
    [Join13].[Id13] AS [Id12], 
    [Join13].[Id14] AS [Id13]
    FROM  [dbo].[Products] AS [Extent1]
    INNER JOIN  (SELECT [Extent2].[Id] AS [Id1], [Extent2].[CompatibleMemory] AS [CompatibleMemory], [Extent2].[WhiteBalance] AS [WhiteBalance], [Extent2].[SceneModes] AS [SceneModes], [Extent2].[ShootingModes] AS [ShootingModes], [Extent2].[PhotoEffects] AS [PhotoEffects], [Extent2].[CameraPlayback] AS [CameraPlayback], [Extent2].[Tripod] AS [Tripod], [Extent2].[DirectPrinting] AS [DirectPrinting], [Extent2].[Colour] AS [Colour], [Extent3].[Id] AS [Id2], [Extent4].[Id] AS [Id3], [Extent5].[Id] AS [Id4], [Extent6].[Id] AS [Id5], [Extent7].[Id] AS [Id6], [Extent8].[Id] AS [Id7], [Extent9].[Id] AS [Id8], [Extent10].[Id] AS [Id9], [Extent11].[Id] AS [Id10], [Extent12].[Id] AS [Id11], [Extent13].[Id] AS [Id12], [Extent14].[Id] AS [Id13], [Extent15].[Id] AS [Id14]
        FROM              [dbo].[CameraAttributes] AS [Extent2]
        LEFT OUTER JOIN [dbo].[CameraAttributeAudio] AS [Extent3] ON [Extent2].[Id] = [Extent3].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeBattery] AS [Extent4] ON [Extent2].[Id] = [Extent4].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeDimensions] AS [Extent5] ON [Extent2].[Id] = [Extent5].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeDisplay] AS [Extent6] ON [Extent2].[Id] = [Extent6].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeLightExposure] AS [Extent7] ON [Extent2].[Id] = [Extent7].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeFlash] AS [Extent8] ON [Extent2].[Id] = [Extent8].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeFocusing] AS [Extent9] ON [Extent2].[Id] = [Extent9].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeInterface] AS [Extent10] ON [Extent2].[Id] = [Extent10].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeLens] AS [Extent11] ON [Extent2].[Id] = [Extent11].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeNetwork] AS [Extent12] ON [Extent2].[Id] = [Extent12].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributePicture] AS [Extent13] ON [Extent2].[Id] = [Extent13].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeShutter] AS [Extent14] ON [Extent2].[Id] = [Extent14].[AttributeId]
        LEFT OUTER JOIN [dbo].[CameraAttributeVideo] AS [Extent15] ON [Extent2].[Id] = [Extent15].[AttributeId] ) AS [Join13] ON [Extent1].[AttributeId] = [Join13].[Id1]
    WHERE [Extent1].[Id] = @EntityKeyValue1


-- EntityKeyValue1: '2' (Type = Int32, IsNullable = false)

-- Executing at 19/04/2016 12:32:01 +01:00

-- Completed in 0 ms with result: SqlDataReader



Closed connection at 19/04/2016 12:32:01 +01:00

1 个答案:

答案 0 :(得分:1)

您可以使用Eager Load

在EF中Include<T>加入实体

像这样:

using (var context = new BloggingContext()) 
{ 
    // Load all blogs and related posts 
    var blogs1 = context.Product
                      .Include(p => p.Category ) 
                      //more included properties
                      .Where(....) //filtering                          
                      .ToList(); //ToList() eagerly loads the Included entites
}