I get this error when i try to request this Controller
The errror message
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 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.speciality.Doctor.Id.
The Controller
public IActionResult List(int id)
{
var doc = _context.Doctors.Include(t => t.speciality).ToList();
return Json(doc);
}
Here is the dataContext and models
public class AppDbContext: DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Speciality> Specialities { get; set; }
}
public class Doctor
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int SpecialityId { get; set; }
public Speciality speciality { get; set; }
}
public class Speciality
{
public int SpecialityId { get; set; }
public string SpecialityName { get; set; }
public ICollection<Doctor> Doctor { get; set; }
}
I tried using Async and await at first and it gave the same error
I installed Microsoft.AspNetCore.Mvc.NewtonsoftJson and still get this error
Hatem Aiman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.