我是 dotnet 的新手,正在尝试 dotnet 6 最小 API。我有两个模型:
namespace Linker.Models
{
class Link : BaseEntity
{
[MaxLength(2048)]
public string Url { get; set;} = default!;
[MaxLength(65536)]
public string? Description { get; set; }
[Required]
public User Owner { get; set; } = default!;
[Required]
public Space Space { get; set; } = default!;
}
}
和:
namespace Linker.Models
{
class Space : BaseEntity
{
public string Name { get; set; } = default!;
public string Code { get; set; } = default!;
public User Owner { get; set; } = default!;
public List<Link> Links { get; set; } = new List<Link>();
}
}
现在,当我尝试序列化Space
模型时,我得到了错误System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64.
(有意义,因为Path: $.Links.Space.Links.Space.Links.Space.Links.Space.Links.Space.Links...
)。是否可以防止 dotnet 将对象序列化这么深?我什至不需要 dotnet 来尝试序列化这么深的关系