Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Entity composed of child entities in Entity Framework
    primarykey
    data
    text
    <p>I have some 'complex' entities formed by other entities. For Example the 'Order' Entity:</p> <ul> <li>Order</li> <li>OrderDetail (child)</li> <li>OrderDetailsDiscount (child of the child)</li> <li>OrderPayment</li> <li>OrderState</li> </ul> <p>The code for the Order class:</p> <pre><code>[MetadataType(typeof(OrderMetadata))] public partial class Order { public OrderPaymentStatus PaymentStatus { get { return Paid ? OrderPaymentStatus.Paid : OrderPaymentStatus.Pending; } } public bool Paid { get { return TotalPaid &gt;= Total; } } public decimal TotalPaid { get { return OrderPayments.Sum(p =&gt; p.Amount); } } public decimal TotalRefund { get { return OrderRefunds.Sum(p =&gt; 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 =&gt; t.Taxes): 0; } } public decimal SubTotal { get { return OrderDetails.Sum(o =&gt; o.Total) + TotalTax; } } public decimal DiscountOffers { get { return OrderDetails.Sum(o =&gt; o.DiscountOffers); } } public bool HasOffers { get { return DiscountOffers &gt; 0; } } public decimal SurchargePercentage { get { return OrderSurcharges.Sum(o =&gt; o.ChargePercentage); } } public decimal DiscountPercentage { get { return OrderDiscounts.Where(o =&gt; o.Type == (int)DiscountType.Percentage).Sum(o =&gt; o.Value); } } public decimal DiscountAbsolute { get { return OrderDiscounts.Where(o =&gt; o.Type == (int)DiscountType.Absolute).Sum(o =&gt; o.Value); } } } </code></pre> <p>I dinamically calculate the order total so I allways need OrderDetail.* loaded from start to avoid multiple queries to the DB when presentig a list of 1000 orders for example so I have implemented this method in my generic repository :</p> <pre><code> public virtual IQueryable&lt;T&gt; GetAllIncluding(params Expression&lt;Func&lt;T, object&gt;&gt;[] includeProperties) { IQueryable&lt;T&gt; query = Fetch(); foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query; } </code></pre> <p>And I call it form my code like this :</p> <pre><code> private IEnumerable&lt;Order&gt; GetAllOrders() { return unitOfWork.OrderRepository.GetAllIncluding( o =&gt; o.OrderDiscounts, o =&gt; o.OrderPayments, o =&gt; o.OrderSurcharges, o =&gt; o.OrderStates, o =&gt; o.OrderRefunds, o =&gt; o.OrderDetails, o =&gt; o.OrderDetails.Select(d =&gt; d.OrderDetailDiscounts), o =&gt; o.OrderDetails.Select(d =&gt; d.OrderDetailOffers), o =&gt; o.User, o =&gt; o.Employee, o =&gt; o.Store, o =&gt; o.TerminalSession); } </code></pre> <p>Ok .. It works.. the problem is when I have to query other complex objects with this one, I have the User Entity wich is composed of other child entities for example:</p> <ul> <li>User</li> <li>UserPoint</li> <li>UserState</li> <li>...</li> </ul> <p>Is there any way to instruct EF to load a group of entities as a whole so I can query using Orders and Users fully loaded to aviod hitting the db when requesting lots of records (I don't want to disable lazy loading) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload