项目场景:
ABP 级联查询序列化时循环引用报错
问题描述:
[Table("School")]
public class School : Entity<int>
{
public string Name { get; set; }
public virtual List<Student> Students { get; set; }
}
[Table("Student")]
public class Student : Entity<int>
{
[Required]
public virtual School School { get; set; }
public string Name { get; set; }
}
实体是这样,序列化时,School中有Students,Student中有School,序列化时会造成循环引用的错误。
解决方案:
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
在Host项目的Startup.cs的ConfigureServices方法中,添加上options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;,设置循环引用不序列化。