我必须在遗留数据库之上映射我的类。
这是类图
当我需要将订单送到订单时,EF会生成以下查询
exec sp_executesql N'SELECT
[Join17].[cd_order_items1] AS [cd_order_items],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN ''1X0X0X'' WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN ''1X0X1X'' WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN ''1X1X'' WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN ''1X2X'' WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN ''1X3X'' WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN ''1X4X'' WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN ''1X5X'' WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN ''1X6X'' WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN ''1X7X'' WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN ''1X8X0X'' WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN ''1X8X1X'' WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN ''1X8X2X'' WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN ''1X9X'' WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN ''1X10X0X'' ELSE ''1X10X1X'' END AS [C1],
[Join17].[cd_quantity] AS [cd_quantity],
[Join17].[vl_item] AS [vl_item],
[Join17].[vl_total] AS [vl_total],
[Join17].[vl_discount] AS [vl_discount],
[Join17].[vl_cost_price] AS [vl_cost_price],
[Join17].[vl_net] AS [vl_net],
[Join17].[cd_status] AS [cd_status],
[Join17].[fl_active] AS [fl_active],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN [Join17].[fl_virtual] WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN [Join17].[fl_virtual] WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN CAST(NULL AS bit) WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN CAST(NULL AS bit) END AS [C2],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN [Join17].[cd_card_type] WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN [Join17].[cd_card_type] WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN CAST(NULL AS smallint) WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN CAST(NULL AS smallint) END AS [C3],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN [Join17].[nr_document] WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN [Join17].[nr_document] WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN CAST(NULL AS varchar(1)) WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN CAST(NULL AS varchar(1)) END AS [C4],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN [Join17].[cd_card] WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN CAST(NULL AS bigint) WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN CAST(NULL AS bigint) END AS [C5],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN [Join17].[cd_order_customization1] WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN [Join17].[cd_order_customization2] WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN [Join17].[cd_order_customization3] WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN [Join17].[cd_order_customization4] WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN [Join17].[cd_order_customization5] WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN [Join17].[cd_order_customization6] WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN [Join17].[cd_order_customization7] WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN [Join17].[cd_order_customization8] WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN [Join17].[cd_order_customization9] WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN [Join17].[cd_order_customization10] WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN [Join17].[cd_order_customization11] WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN [Join17].[cd_order_customization12] WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN [Join17].[cd_order_customization13] WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN [Join17].[cd_order_customization14] ELSE [Join17].[cd_order_customization15] END AS [C6],
[Join17].[cd_order] AS [cd_order],
[Join17].[cd_product] AS [cd_product],
[Join17].[cd_order_item_base] AS [cd_order_item_base],
CASE WHEN ([Join17].[Discriminator1] = N''CardOrderItem'') THEN [Join17].[Id1] WHEN ([Join17].[Discriminator1] = N''CustomCardOrderItem'') THEN [Join17].[Id2] WHEN ([Join17].[Discriminator1] = N''InterestOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''NotChargedReplacementOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''PackageOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''PhotoOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''RechargeFeeOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''RechargeOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''ReplacementFeeOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''EmailSendPasswordOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''LetterSendPasswordOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''SMSSendPasswordOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''ServiceFeeOrderItem'') THEN CAST(NULL AS int) WHEN ([Join17].[Discriminator1] = N''ExtraShippingOrderItem'') THEN CAST(NULL AS int) END AS [C7]
FROM [dbo].[tbl_order_item] AS [Extent1]
INNER JOIN (SELECT [Extent2].[cd_order_items] AS [cd_order_items1], [Extent2].[cd_quantity] AS [cd_quantity], [Extent2].[vl_item] AS [vl_item], [Extent2].[vl_total] AS [vl_total], [Extent2].[vl_discount] AS [vl_discount], [Extent2].[vl_cost_price] AS [vl_cost_price], [Extent2].[vl_net] AS [vl_net], [Extent2].[cd_status] AS [cd_status], [Extent2].[fl_active] AS [fl_active], [Extent2].[fl_virtual] AS [fl_virtual], [Extent2].[cd_card_type] AS [cd_card_type], [Extent2].[nr_document] AS [nr_document], [Extent2].[cd_card] AS [cd_card], [Extent2].[Discriminator] AS [Discriminator1], [Extent2].[cd_order] AS [cd_order], [Extent2].[cd_product] AS [cd_product], [Extent2].[cd_order_item_base] AS [cd_order_item_base], [Extent3].[cd_order_customization] AS [cd_order_customization1], [Extent4].[Id] AS [Id1], [Extent5].[cd_order_customization] AS [cd_order_customization2], [Extent6].[Id] AS [Id2], [Extent7].[cd_order_customization] AS [cd_order_customization3], [Extent8].[cd_order_customization] AS [cd_order_customization4], [Extent9].[cd_order_customization] AS [cd_order_customization5], [Extent10].[cd_order_customization] AS [cd_order_customization6], [Extent11].[cd_order_customization] AS [cd_order_customization7], [Extent12].[cd_order_customization] AS [cd_order_customization8], [Extent13].[cd_order_customization] AS [cd_order_customization9], [Extent14].[cd_order_customization] AS [cd_order_customization10], [Extent15].[cd_order_customization] AS [cd_order_customization11], [Extent16].[cd_order_customization] AS [cd_order_customization12], [Extent17].[cd_order_customization] AS [cd_order_customization13], [Extent18].[cd_order_customization] AS [cd_order_customization14], [Extent19].[cd_order_customization] AS [cd_order_customization15]
FROM [dbo].[tbl_order_item] AS [Extent2]
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent3] ON ([Extent3].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent3].[cd_order_items])
LEFT OUTER JOIN [dbo].[OrderItemShipments] AS [Extent4] ON [Extent2].[cd_order_items] = [Extent4].[OrderItem_Id]
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent5] ON ([Extent5].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent5].[cd_order_items])
LEFT OUTER JOIN [dbo].[OrderItemShipments] AS [Extent6] ON [Extent2].[cd_order_items] = [Extent6].[OrderItem_Id]
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent7] ON ([Extent7].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent7].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent8] ON ([Extent8].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent8].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent9] ON ([Extent9].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent9].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent10] ON ([Extent10].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent10].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent11] ON ([Extent11].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent11].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent12] ON ([Extent12].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent12].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent13] ON ([Extent13].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent13].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent14] ON ([Extent14].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent14].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent15] ON ([Extent15].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent15].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent16] ON ([Extent16].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent16].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent17] ON ([Extent17].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent17].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent18] ON ([Extent18].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent18].[cd_order_items])
LEFT OUTER JOIN [dbo].[tbl_order_item_customization] AS [Extent19] ON ([Extent19].[cd_order_items] IS NOT NULL) AND ([Extent2].[cd_order_items] = [Extent19].[cd_order_items]) ) AS [Join17] ON [Extent1].[cd_order_items] = [Join17].[cd_order_items1]
WHERE ([Extent1].[cd_order_item_base] IS NOT NULL) AND ([Extent1].[cd_order_item_base] = @EntityKeyValue1) AND ([Join17].[Discriminator1] IN (N''CardOrderItem'',N''CustomCardOrderItem'',N''InterestOrderItem'',N''NotChargedReplacementOrderItem'',N''PackageOrderItem'',N''PhotoOrderItem'',N''RechargeFeeOrderItem'',N''RechargeOrderItem'',N''ReplacementFeeOrderItem'',N''EmailSendPasswordOrderItem'',N''LetterSendPasswordOrderItem'',N''SMSSendPasswordOrderItem'',N''ServiceFeeOrderItem'',N''ExtraShippingOrderItem'',N''RegularShippingOrderItem''))',N'@EntityKeyValue1 int',@EntityKeyValue1=308633
我能做些什么来产生更好的查询?如果我有很多物品,那就太慢了
答案 0 :(得分:2)
快速获胜可能是创建一个以更有效的方式连接表的SQL视图,然后使用EF查询视图。
我已成功多次使用此策略。