[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,序列化时会造成循环引用的错误。
解决方法:
在Host项目的Startup.cs的ConfigureServices方法中,添加上图红框中的代码,设置循环引用不序列化。
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;