早期加载dotnet实体框架

时间:2019-07-13 06:11:07

标签: c# .net entity-framework

我有3个模型类,如下所示-

    class Product
    {
      ...
    }

    class OrderDetails
    {
      ...
      public int ProductId { get; set; }
      [ForeignKey("ProductId")]
      public virtual Product Product { get; set; }

      public int OrderId { get; set; }
      [ForeignKey("OrderId")]
      public virtual Order Order { get; set; }
    }

    class Order
    {
      ...
      public virtual ICollection<OrderDetails> OrderDetailsList { get; set; }
    }

当我从上下文中获取详细信息时,如下所示-

Order order = await _dbContext.Orders.Include(x => x.OrderDetailsList).FirstOrDefault(x => x.Id == orderId);

当我获取Product时,我也希望尽早为OrderDetailsList中的每个数据加载Order,但是我无法实现。

2 个答案:

答案 0 :(得分:0)

这行吗?

    for F in (Frame2, ):
        frame = F(container, self)
        self.frames[F] = frame
        frame.pack(fill='both', expand=True)

答案 1 :(得分:0)

这对我来说很好,请尝试一下。

 _dbContext.Orders.Include(s => s.OrderDetailsList.Select(e => e.Product));