I've encountered the error:
JsonSerializationException: Self referencing loop detected for
property 'Subject' with type 'Project.Models.Subject'. Path
'data[0].Totals'.
It occurs when I load a View with a dataGrid populated by an IEnumerable<Subject>
model. The Grid is a DevExtreme DataGrid bound to the View's model like this:
@(Html.DevExtreme().DataGrid()
.DataSource(Model)
.Paging(paging =>
{
paging.Enabled(true);
paging.PageIndex(0);
paging.PageSize(20);
})
.Columns(columns =>
{
columns.Add().DataField("SubjectId");
... other fields
})
)
Which is populated from a Controller that pulls data from a Repository with this function:
public async Task<IEnumerable<Subject>> GetSubjectsAsync()
{
return await _context.Subject.ToListAsync();
}
The Subject table has a 1:1 relationship with Totals with Totals having a foreign key reference to Subject. The Models in the project look like this (generated from Scaffold-DbContext):
public partial class Subject
{
public Guid SubjectId { get; set; }
public virtual Totals Totals { get; set; }
}
public partial class Totals
{
public Guid TotalsId { get; set; }
public virtual Subject Subject { get; set; }
}
Since the 2 objects reference eachother it causes a loop when serializing it. To correct this I added this config to my Startup.ConfigureServices method:
services.AddMvc()
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
Which I got from this answer: https://stackoverflow.com/a/40501464/7897176
However this doesn't fix the problem and its still causing an error when I load a view that involves Subjects. Adding [JsonIgnore]
to the Subject property of Totals fixes the problem, but I don't want to have to add that to every child property in my models and have to redo it whenever I update my models from the db.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…