.Net4でEF5を使用しています
私は次のモデルを持っています:
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; }
}
およびモデル構成:
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);
}
次のクエリを実行すると、19の結合でSQLが取得されますが、これは単純な関係には過剰に思えます
context.Orders
.Where(x => x.OrderId == orderId)
.Include(x => x.OrderLines)
.Include(x => x.BillingAddress)
.Include(x => x.DeliveryAddress)
.FirstOrDefault();
私は何か間違ったことをしていますか?生成されたSQLを最適化するためにlinqクエリをフォーマットする方法は異なりますか?
編集:
これは実際のクエリです:https ://gist.github.com/4278014