【问题标题】:EF Core does not include all the child itemsEF Core 不包括所有子项
【发布时间】:2019-02-02 11:12:46
【问题描述】:

我正在使用 EF Core 和 .NET Core 2.0。

我有这个实体层次结构:

  • 订购

    • 订单项

      • 产品

我在服务中的 LINQ 查询有效,但只返回一个 OrderItem,而我有 5 个。它还很好地返回了该 OrderItem 的产品。 所以我想要的是,修改这个查询以包含我对特定订单 ID 的所有 OrderItems,而不仅仅是第一个项目。我期待包含为我完成这项工作。

public class Order
{
    public int Id { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal TotalPrice { get; set; }
    public List<OrderItem> OrderItems { get; set; }

    public decimal CalculateTotal()
    {
        return OrderItems.Sum(item => item.GetPrice());
    }
}

public class OrderItem
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public Product Product { get; set; }
    public Order Order{ get; set; }

    public decimal GetPrice()
    {
        return Product.Price * Quantity;
    }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}

这是带有查询的服务:

public class OrderRepository: IOrderRepository
{
    private readonly BasketDbContext _basketDbContext;
    public OrderRepository(BasketDbContext basketDbContext)
    {
        _basketDbContext = basketDbContext;
    }

    public IEnumerable<Order> GetAllOrders()
    {
        return _basketDbContext.Orders
            .Include(x => x.OrderItems)
            .ThenInclude(y => y.Product).ToList();
    }
}

这是我从 GetAllOrders 方法得到的 JSON:

[  
    {  
      "id":1,
      "orderDate":"2019-02-02T11:24:36.103",
      "totalPrice":0.0000,
      "orderItems":[  
         {  
            "id":1,
            "orderId":1,
            "productId":1,
            "quantity":1,
            "product":{  
               "id":1,
               "name":"Samsung Galaxy S8 64GB",
               "description":"5.8-Inch, Dual pixel 12MP camera",
               "price":390.0000
            }

如您所见,JSON 的格式也不是很好,它不会关闭初始的 [{。

我做错了什么? 感谢您的帮助

【问题讨论】:

    标签: c# linq .net-core asp.net-core-webapi ef-core-2.0


    【解决方案1】:

    这看起来像在序列化的最后一步中,您的序列化程序无法继续,因此只是退出并且 ASP.NET Core 将未完成的响应发送到客户端。这可能是因为您从OrderItemOrder 的反向引用。假设您使用的是 Newtonsoft 的 Json.NET,请尝试在您的 Startup.cs 中将 JsonSerializerSettings 设置为 ReferenceLoopHandling = ReferenceLoopHandling.Ignore

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvc()
        .AddJsonOptions(
            o => o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        );
    }
    

    另一种选择是简单地将OrderItem 中的Order 从序列化中排除,例如但是,通过[JsonIgnore] 属性,这当然意味着它永远不会出现在您的任何回复中。

    【讨论】:

    • 我从 OrderItem 中删除了 Order 属性,现在它运行良好,非常感谢!
    • 很高兴它有帮助 :) 仅供参考:当代码在内存中时,您可以有循环依赖,但是一旦进入序列化,循环依赖需要在某一时刻以某种方式“硬切”(例如以上述方式)。
    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 2018-05-06
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多