【问题标题】:Controller crash when returning ef core objects with navigation properties返回具有导航属性的 ef 核心对象时控制器崩溃
【发布时间】:2019-01-05 17:43:29
【问题描述】:

我的环境是 ASP.NET 核心 2.1 EF Core 2.1

public class Customer
{
    public int Id { get; set; }

    public string name { get; set; }
    public virtual ICollection<CustomerLocation> CustomerLocations { get; set; }


public class CustomerLocation
{
    public int Id { get; set; }
    public int customerId { get; set; }
    public string streetAddress { get; set; }
    public string zipCode { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string category { get; set; }
    public virtual Customer Customer { get; set; }
}

在我的 Api 控制器中

    // GET: api/Customers
    [HttpGet]
    public IEnumerable<Customer> GetCustomers()
    {
        var custlist = _context.Customers
            .Include(c=>c.CustomerLocations)
            .ToList();

        return custlist;

    }

我想收到这个 JSON

[
{
    id: 1,
    name: "My First Company",
    customerLocations: [
    {
        id: 1,
        customerId: 1,
        streetAddress: "13 Union Street",
        zipCode: "94111",
        city: "San Francisco",
        state: "CA",
        category: "Headquarter",
        customer: null
    },
    {
        id: 2,
        customerId: 1,
        streetAddress: "1098 Harrison St",
        zipCode: "94103",
        city: "San Francisco",
        state: "CA",
        category: "Warehouse",
        customer: null
    }]
},
{
    id: 2,
    name: "Another Company",
    customerLocations: [ ]
}
]

但我收到的答案是

[
{
    id: 1,
    name: "My First Company",
    customerLocations: [
    {
        id: 1,
        customerId: 1,
        streetAddress: "13 Union Street",
        zipCode: "94111",
        city: "San Francisco",
        state: "CA",
        category: "Headquarter"

然后它在尝试循环进入“customerLocation”的“customer”导航属性时崩溃。

我发现摆脱这种情况的唯一方法是将每个 CustomerLocation 中的所有“客户”引用显式设为 null,但我无法相信这是处理此问题的正确方法。

【问题讨论】:

  • 有什么异常?
  • @ofiris 我不能例外。如果我调试控制器执行一切正常,直到执行 return 语句。然后在浏览器中,我只有一个在“客户”属性之前被截断的 JSON 结构。如果我使用 Postman & Fiddler,我会收到此响应“[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 530 bytes.”
  • 你可能会在这里找到解决方案stackoverflow.com/questions/2002940/…
  • @karan 感谢您的参考,但似乎已经过时(8 年前);这一定是 EF 核心 2.1 的问题,我在 EF6 上没有这个问题。
  • 在序列化模型时,JSON.Net 发现循环引用并中断。您可以使用 [JsonIgnore] 属性在 CustomerLocation 类中装饰“客户”属性,也可以使用 @Stackoverflow post 描述的配置来处理此问题。另外,请阅读 this blog post 了解与这两种方法相关的问题。

标签: c# asp.net-core ef-core-2.1


【解决方案1】:

这个错误的原因是序列化Customer时出现引用循环,正如你所说,当你将客户引用设置为null时,你避免了引用循环。

您可以处理它的另一种方法是在startup.cs 中为Json 序列化程序设置ReferenceLoopHandling

services
    .AddMvc()
    .AddJsonOptions(config =>
    {
        config.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多