我正在加载ServiceTrips
计划日历,想知道是否有最快的方法可以从许多表中急切地加载相关数据。
这是需要加载的模型(映射是每个具体类型的表)
public class ServiceTrip : BaseEntity
{
public ICollection<Employee> Crew { get; set; }
public ICollection<ServiceAssignment> Assignments { get; set; }
}
public class ServiceAssignment : BaseEntity
{
public ServiceOrder ServiceOrder { get; set; }
public DeliveryOrder DeliveryOrder { get; set; }
}
public class ServiceOrder : OrderBase { }
public class DeliveryOrder : OrderBase { }
public abstract class OrderBase : BaseEntity
{
public ICollection<ServiceAssignment> Assignments { get; set; }
public Sale Sale { get; set; }
}
public class Sale : BaseEntity
{
public Employee Manager { get; set; }
public Customer Customer { get; set; }
public ICollection<ServiceOrder> ServiceOrders { get; set; }
public ICollection<DeliveryOrder> DeliveryOrders { get; set; }
}
public class Employee : BaseEntity { }
public class Customer : BaseEntity { }
public abstract class BaseEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
我基本上已经尝试过这样的事情,但不知道从哪里开始。
var tripsQuery = db.ServiceTrips
.Where(x => x.StartDate >= FirstDay && x.StartDate <= LastDay)
.Include(x => x.Crew)
.Include(x => x.ServiceAssignments)
.Include(x => x.ServiceAssignments.Select(y => y.DeliveryOrder))
.Include(x => x.ServiceAssignments.Select(y => y.ServiceOrder))
.Include(x => x.ServiceAssignments.Select(y => y.DeliveryOrder.Sale))
.Include(x => x.ServiceAssignments.Select(y => y.ServiceOrder.Sale))
.Include(x => x.ServiceAssignments.Select(y => y.DeliveryOrder.Sale.Customer))
.Include(x => x.ServiceAssignments.Select(y => y.ServiceOrder.Sale.Customer))
.Include(x => x.ServiceAssignments.Select(y => y.DeliveryOrder.Sale.Manager))
.Include(x => x.ServiceAssignments.Select(y => y.ServiceOrder.Sale.Manager))
;
该模型已针对该问题进行了简化。在生产中,我从大约 20 张桌子中抽出。加载大约需要 10-15 秒。我尝试每天异步加载,但这增加了加载的总时间。