I'm loading ServiceTrips for a scheduling calendar and wondering if there is fastest approach for eagerly loading the related data from many tables.
Here is the model that needs to be loaded (the mapping is table per concrete type)
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; }
}
I've basically tried things like this and don't know where to start.
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))
;
The model is simplified for the question. In production I'm pulling from about 20 tables. It takes about 10-15 seconds to load. I tried loading each day asynchronously but that increased the total time to load.
virtualI would assert that the data is eager loading without any of theInclude's