简体   繁体   English

如何使用3来优化实体框架查询

[英]How to optimise an Entity framework query with 3 includes

I'm using EF 5 on .Net 4 我在.Net 4上使用EF 5

I have the following model: 我有以下模型:

public class Order
{

    [Key]
    public int Id { get; set; }
    public string OrderId { get; set; }

    public Address BillingAddress { get; set; }
    public Address DeliveryAddress { get; set; }
    public ICollection<OrderLine> OrderLines { get; set; }
}

public class OrderLine
{
    [Key]
    public int Id { get; set; }

    public string Description { get; set; }
    public decimal UnitPrice { get; set; }
    public int Quantity { get; set; }
    public string SKU { get; set; }
    public decimal ShippingCost { get; set; }
    public decimal Tax { get; set; }
}

public class Address
{
    [Key]
    public int Id { get; set; }
    public string Addressee { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Country { get; set; }
    public string Postcode { get; set; }
}

and model configuration: 和型号配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>().HasMany(x => x.OrderLines).WithRequired().WillCascadeOnDelete();
    modelBuilder.Entity<Order>().HasOptional(x => x.BillingAddress).WithOptionalDependent().WillCascadeOnDelete();
    modelBuilder.Entity<Order>().HasOptional(x => x.DeliveryAddress).WithOptionalDependent().WillCascadeOnDelete();

    base.OnModelCreating(modelBuilder);
}

when I run the following query I get sql with 19 Joins which seems excessive for the simple relationship 当我运行以下查询时,我得到带有19个Join的 sql,对于简单的关系而言似乎过多

context.Orders
       .Where(x => x.OrderId == orderId)
       .Include(x => x.OrderLines)
       .Include(x => x.BillingAddress)
       .Include(x => x.DeliveryAddress)
       .FirstOrDefault();

Am I doing something wrong? 难道我做错了什么? Is there a different to format the linq query to optimize the produced SQL? 格式化linq查询以优化生成的SQL是否有其他格式?

EDIT: 编辑:

This is the actual query: https://gist.github.com/4278014 这是实际的查询: https : //gist.github.com/4278014

I think the problem is with the WithOptionalDependent on the billing and delivery addresses. 我认为问题在于帐单和收货地址上的WithOptionalDependent This creates a 1:1 relationship between the address and the order, even though the tables are set up for a 1:many relationship. 即使将表设置为1:1:1关系,这也会在地址和顺序之间创建1:1关系。

If you change the configuration to use WithMany instead: 如果将配置更改为使用WithMany

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>().HasMany(x => x.OrderLines).WithRequired().WillCascadeOnDelete();
    modelBuilder.Entity<Order>().HasOptional(x => x.BillingAddress).WithMany().WillCascadeOnDelete();
    modelBuilder.Entity<Order>().HasOptional(x => x.DeliveryAddress).WithMany().WillCascadeOnDelete();

    base.OnModelCreating(modelBuilder);
}

the query is much simpler, and is essentially equivalent to: 查询要简单得多,基本上等同于:

...
FROM [Orders]
LEFT OUTER JOIN [Addresses] As B ON [Orders].[BillingAddress_Id] = B.[Id]
LEFT OUTER JOIN [Addresses] As D ON [Orders].[DeliveryAddress_Id] = D.[Id]
LEFT OUTER JOIN [OrderLines] ON [Orders].[Id] = [OrderLines].[Order_Id]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM