0

我有一些由其他实体组成的“复杂”实体。例如“订单”实体:

  • 命令
  • 订单详情(子)
  • OrderDetailsDiscount(孩子的孩子)
  • 订单支付
  • 订单状态

Order 类的代码:

[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);
        }
    }
}

我以动态方式计算订单总数,因此我总是需要从一开始就加载 OrderDetail.*,以避免在呈现 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);
    }

好的..它有效..问题是当我必须用这个查询其他复杂对象时,我的用户实体由其他子实体组成,例如:

  • 用户
  • 用户点
  • 用户状态
  • ...

有什么方法可以指示 EF 将一组实体作为一个整体加载,这样我就可以使用完全加载的 Orders 和 Users 进行查询,以避免在请求大量记录时访问数据库(我不想禁用延迟加载)

4

1 回答 1

0
context.Entity.Include("ChildEntity");

或者

context.Entity.Include( x => x.ChildEntity);

如果您需要更多嵌套实体

context.Entity.Include( x => x.ChildEntity.Select( c => c.ChildChildEntity ));

请记住,lambda 表达式存在于System.Data.Entity命名空间中。

请记住,通常太多的连接比多个数据库调用更糟糕。

于 2014-07-16T19:44:58.010 回答