3

I have two tables Appointment and TaskAllocation having one to many relationship. now when i get the Appointment

public IEnumerable<Appointment> GetAppointments(int employeeId, DateTime date)
    {
        return _context.Appointment.Where(a => a.EmployeeId == employeeId && 
            a.AppointmentDate == date)
           .Include(a=>a.Tasks).ToList();
    }

It causes including one appointment with many tasks and again one task with that appointment with many tasks and so on.

Noman Fareed
  • 274
  • 3
  • 11
  • I had the same issue and from looking at the output I noticed there were no calls to load the data after the initial call to get the data. – chillfire Jun 13 '19 at 11:09

1 Answers1

5

In your ConfigureService, you need to Add Json Options for handling reference loop handling

.AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
    options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
});

or you may choose to directly ignore reference loops by

options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72