Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF核心不包括所有子项_C#_Linq_.net Core_Asp.net Core Webapi_Ef Core 2.0 - Fatal编程技术网

C# EF核心不包括所有子项

C# EF核心不包括所有子项,c#,linq,.net-core,asp.net-core-webapi,ef-core-2.0,C#,Linq,.net Core,Asp.net Core Webapi,Ef Core 2.0,我正在使用EF Core和.NET Core 2.0 我有这个实体层次结构: 命令 订单项 产品 我的LINQ查询在服务中工作,但只返回一个OrderItem,我有5个。它还可以很好地返回该OrderItem的产品。 因此,我想要的是,修改这个查询,使其包含特定订单Id的所有OrderItems,而不仅仅是第一个项目。我期待着包括为我做那项工作 public class Order { public int Id { get; set; } public DateTi

我正在使用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; }
}
正如您所看到的,JSON的格式也不是很好,它没有关闭初始值[{

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

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

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

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

我从OrderItem中删除了Order属性,它现在工作得很好,非常感谢!很高兴它有帮助:)仅供参考:虽然代码在内存中,但您可以具有循环依赖关系,但一旦它进入序列化,循环依赖关系就需要被删除在某一点上以某种方式“硬切”(例如,以上述方式)。
[  
    {  
      "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
            }
public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc()
    .AddJsonOptions(
        o => o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    );
}