[英]How to load nested entities without lazy loading in Entity Framework?
I have the following model: 我有以下模型:
public class Order
{
public int Id {get; set;}
public int Number {get; set;}
public virtual ICollection<OrderDetail> Details {get; set;}
}
public class OrderDetail
{
public int Id {get; set;}
public int OrderId {get; set;}
public virtual Product Product {get; set;}
}
public class Product
{
public int Id {get; set;}
public int Number {get; set;}
public string Description {get; set;}
}
In my OrderRepository, I load a complete order like this: 在我的OrderRepository中,我加载了一个完整的订单,如下所示:
public override Order Get(int id)
{
return base.Get(id, x => x.Details);
}
And the base method is: 基本方法是:
public virtual T Get(int id, params Expression<Func<T, object>>[] include)
{
if (include.Any())
{
var set = include.Aggregate<Expression<Func<T, object>>, IQueryable<T>>
(dbSet, (current, expression) => current.Include(expression));
return set.SingleOrDefault<T>(x => x.Id == id);
}
return dbSet.Find(id);
}
The above works partially fine because it loads the Order and the OrderDetails. 上面的部分工作正常,因为它加载了Order和OrderDetails。 However, I also want to load the related Product for every Detail so that I can display the product description in the list too.
但是,我也想为每个详细信息加载相关的产品,以便我也可以在列表中显示产品说明。
How can I improve on the above method to allow me to do so? 如何改进上述方法以允许我这样做?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.