【发布时间】:2021-12-14 11:51:47
【问题描述】:
我有两个模型,服务器和更新。这两个类之间的关系是 1...*N,这意味着每个 Server 有多个更新。 这是我的服务器模型:
public class Server
{
public Server()
{
UpdateList = new HashSet<Update>();
}
[Key]
[Required]
public int ID { get; set; }
[Required]
public string ComputerName { get; set; }
[Required]
public string Type { get; set; }
[Required]
public string Estado { get; set; }
[Required]
public string Phase { get; set; }
public virtual ICollection<Update> UpdateList { get; set; }
}
这是我的更新模型:
public class Update
{
[Required]
public int ID { get; set; }
public string updateId { get; set; }
public string updateTitle { get; set; }
public string updateDescription { get; set; }
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("Server")]
[Display(Name = "Server")]
public int? ServerFK { get; set; }
public virtual Server Server { get; set; }
}
这是我在邮递员中使用的 POST 请求:
{
"computerName":"ServerTestUpdate",
"type":"ServidorAplicacional",
"estado":"Reboot Pending",
"phase":"0",
"updateList":
[{
"updateId":"idTest",
"updateTitle":"Update1",
"updateDescription":"TestDescription"
}]
}
但是,当我这样做时,我收到以下错误:
System.Text.Json.JsonException:检测到可能的对象循环。 这可能是由于循环或物体深度较大 超过最大允许深度 32。考虑使用 JsonSerializerOptions 上的 ReferenceHandler.Preserve 以支持循环。
语法有问题还是控制器中有问题?
提前谢谢你
【问题讨论】:
-
您可能想要添加属性,说明您不想序列化 Server 属性,因为它会指向父级,该父级指向该父级,该父级指向该父级...我假设这来自序列化,而不是反序列化。
-
我该怎么做?
标签: json asp.net-core asp.net-mvc-4 post postman