我有一个使用Entity Framework 5的查询,它用于填充Order视图。该视图显示完整的订单详细信息,所有订单商品,相关产品,图片,折扣,产品选项等。
下面的查询会生成一个巨大的SQL查询(如您所料),因为它必须返回多行以包含所有子属性,并且性能非常糟糕。
分开这个的最佳方法是什么?我假设我可以将其拆分为多个数据库调用(但是如何?)和/或使用投影来拉取视图模型中所需的属性。
var order = context.Orders
.Include("OrderItems.Product.ProductSubTypeRange.ProductRange.Supplier")
.Include("OrderItems.Product.ProductSubTypeRange.ProductSubType.ProductType")
.Include("OrderItems.Product.ProductMaterial")
.Include("OrderItems.Product.ProductImages.Image")
.Include("OrderItems.Product.ProductImages.ImageLabel")
.Include("OrderItems.OrderItemOptions.ProductOption.ProductOptionType")
.Include("OrderItems.OrderItemOptions.ProductOption.ProductOptionGroup.ProductOptionType")
.Include("OrderSamples.Product.ProductSubTypeRange.ProductRange")
.Include("OrderSamples.Product.ProductSubTypeRange.ProductSubType.ProductType")
.Include("OrderSamples.Product.ProductMaterial")
.Include("OrderSamples.Product.ProductImages.Image")
.Include("OrderSamples.Product.ProductImages.ImageLabel")
.Include("OrderAccessories.Accessory.AccessorySubType.AccessoryType")
.Include("OrderAccessories.Accessory.AccessoryImages.Image")
.Include("OrderAccessories.Accessory.AccessoryImages.ImageLabel")
.Include("OrderAccessories.OrderAccessoryOptions.AccessoryOption.AccessoryOptionType")
.Include("OrderAccessories.OrderAccessoryOptions.AccessoryOption.AccessoryOptionGroup.AccessoryOptionType")
.Include("OrderAccessorySamples.Accessory.AccessorySubType.AccessoryType")
.Include("OrderDiscounts.Discount.DiscountProducts.Product")
.Include("OrderDiscounts.Discount.DiscountProductSubTypes.ProductSubType")
.Include("Customer")
.Include("PaymentTransactions")
.Include("Emails");