简体   繁体   English

EF Core并不包括所有子项目

[英]EF Core does not include all the child items

I am using EF Core and .NET Core 2.0. 我正在使用EF Core和.NET Core 2.0。

I have this entity hierarchy: 我有这个实体层次结构:

  • Order 订购

    • OrderItem OrderItem的

      • Product 产品

My LINQ query in the service works, but only returns one OrderItem and I have 5. It also returns well the product for that OrderItem. 我在服务中的LINQ查询有效,但仅返回一个OrderItem,而我有5个。它也很好地返回了该OrderItem的产品。 So what I want is, modify this query to include actually all the OrderItems I have for a particular order Id, not only the first item. 所以我想要的是,修改此查询以实际上包括特定订单ID的所有OrderItem,而不仅仅是第一项。 I was expecting the include to do that job for me. 我期望include能够为我完成这项工作。

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

and this is the service with the query: 这是带有查询的服务:

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

And this is the JSON I got from the method GetAllOrders: 这是我从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
            }

As you can see, the JSON is also not well formatted, it does not close the initial [{. 如您所见,JSON的格式也不正确,它不会关闭开头的[{。

What I am doing wrong? 我做错了什么? Thanks for the help 谢谢您的帮助

This looks like in the final step of the serialization your serializer is not able to proceed hence just exits and ASP.NET Core sends the unfinished response to the client. 看起来在序列化的最后一步,您的序列化程序无法继续执行,因此仅退出而ASP.NET Core将未完成的响应发送给客户端。 Maybe this happens because of your backreference from OrderItem to Order . 可能是由于您从OrderItemOrder反向引用而发生的。 Assuming you are using Newtonsoft's Json.NET, try setting JsonSerializerSettings to ReferenceLoopHandling = ReferenceLoopHandling.Ignore in your Startup.cs . 假设您正在使用Newtonsoft的Json.NET,请尝试在Startup.cs 中将JsonSerializerSettings设置为ReferenceLoopHandling = ReferenceLoopHandling.Ignore

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

Another option would be to simply exclude the Order in OrderItem from serialization, eg via the [JsonIgnore] attribute, however, this of course means that it will never showup in any of your responses. 另一个选择是简单地从序列化中排除OrderItemOrder ,例如通过[JsonIgnore]属性,但是,这当然意味着它永远不会出现在您的任何响应中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM