0
我有一些由其他實體形成的「複雜」實體。對於實施例 '訂單' 實體:獲取由實體框架中的子實體組成的實體
- 訂單
- 的OrderDetail(子)
- OrderDetailsDiscount(兒童的兒童)
- OrderPayment
- OrderState
爲代碼訂單等級:
[MetadataType(typeof(OrderMetadata))]
public partial class Order
{
public OrderPaymentStatus PaymentStatus {
get { return Paid ? OrderPaymentStatus.Paid : OrderPaymentStatus.Pending; }
}
public bool Paid {
get {
return TotalPaid >= Total;
}
}
public decimal TotalPaid {
get {
return OrderPayments.Sum(p => p.Amount);
}
}
public decimal TotalRefund {
get {
return OrderRefunds.Sum(p => p.Amount);
}
}
public decimal TotalDebt {
get {
return Total - TotalPaid + TotalRefund;
}
}
public decimal TotalDiscounts {
get {
return ((SubTotal * DiscountPercentage)/100) + DiscountAbsolute;
}
}
public decimal TotalSurcharges {
get {
return ((SubTotal * SurchargePercentage)/100);
}
}
[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode = false)]
public decimal Total {
get {
return SubTotal - TotalDiscounts + TotalSurcharges;
}
}
public decimal TotalTax {
get {
return (TaxEnabled) ? OrderDetails.Sum(t => t.Taxes): 0;
}
}
public decimal SubTotal {
get {
return OrderDetails.Sum(o => o.Total) + TotalTax;
}
}
public decimal DiscountOffers {
get {
return OrderDetails.Sum(o => o.DiscountOffers);
}
}
public bool HasOffers {
get {
return DiscountOffers > 0;
}
}
public decimal SurchargePercentage {
get {
return OrderSurcharges.Sum(o => o.ChargePercentage);
}
}
public decimal DiscountPercentage {
get {
return OrderDiscounts.Where(o => o.Type == (int)DiscountType.Percentage).Sum(o => o.Value);
}
}
public decimal DiscountAbsolute
{
get
{
return OrderDiscounts.Where(o => o.Type == (int)DiscountType.Absolute).Sum(o => o.Value);
}
}
}
我dinamically計算訂單總讓我永諾需要的OrderDetail *從開始加載presentig 1000臺的訂單,例如列表時避免多次查詢數據庫,所以我在我的通用倉庫實現了這個方法。
public virtual IQueryable<T> GetAllIncluding(params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = Fetch();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query;
}
而且我把它形成我這樣的代碼:
private IEnumerable<Order> GetAllOrders()
{
return unitOfWork.OrderRepository.GetAllIncluding(
o => o.OrderDiscounts,
o => o.OrderPayments,
o => o.OrderSurcharges,
o => o.OrderStates,
o => o.OrderRefunds,
o => o.OrderDetails,
o => o.OrderDetails.Select(d => d.OrderDetailDiscounts),
o => o.OrderDetails.Select(d => d.OrderDetailOffers),
o => o.User,
o => o.Employee,
o => o.Store,
o => o.TerminalSession);
}
確定..它的工作..問題是,當我要查詢其他複雜的對象與這一個,我的用戶實體至極組成其他子實體例如:
- 用戶
- UserPoint
- UserState
- ...
有什麼辦法來指導EF裝載一組實體作爲一個整體,所以我可以用完全的訂單和用戶查詢當請求大量的記錄時我加載到aviod命中db(我不想禁用延遲加載)
你就不能添加:'O => o.User.GetAllIncluding(O => u.UserPoint,U => u.UserState,...)'? – 2013-04-22 19:21:11
是,TE問題是涉及用戶查詢*和*訂購例如:。未支付的訂單活躍用戶少於100點 – Marc 2013-04-22 20:30:45
對我來說,它看起來像你不應該試圖組成一個一刀切的查詢。你有沒有需要_all_這些包括一個用例?我認爲(但我可能是錯的),您應該使用更多量身定製的查詢,而且(有些)包含更少的內容,有時也可能會有一些聯接。如果你真的想讓你預先加載任何東西。它可能需要一個或兩個查詢,但EF將善意修復關係。但是,我認爲你可以查詢很多數據。 – 2013-04-22 22:00:08