How to patch using JsonPatchDocument on DTO with multiple tables

882 views Asked by At

I'm using ASP .Net Core. I have a DTO with 3 models of table from DB Scaffolding.

public class StudentDTO
{
   public int StudentId { get; set; }
   public Student StudentInfo { get; set; }
   public Address StudentAddressDetails { get; set; }
   public Extracurricular ExActivities { get; set; }
}

I'm able to Patch models using JsonPatchDocument with a separate route like the following:

public async Task<IActionResult> PatchStudent(int studentId, [FromBody] JsonPatchDocument<Student> student)

public async Task<IActionResult> PatchStudentAddress(int studentId, [FromBody] JsonPatchDocument<Address> studentAddress)

public async Task<IActionResult> PatchExActivities(int studentId, [FromBody] JsonPatchDocument<ExtraCurricular> studentExActivities)

I would like to patch in StudentDTO directly. I tried with

public async Task<IActionResult> PatchStudentDetails(int studentId, [FromBody] JsonPatchDocument updateStudentInfo)
{
    StudentDTO student = await GetStudentInfo(int studentId);

    updateStudentInfo.ApplyTo(student.StudentInfo);
    updateStudentInfo.ApplyTo(student.StudentAddressDetails); //lin 2
    updateStudentInfo.ApplyTo(student.ExActivities); //line 3
}

If I try to update StudentInfo, I get segment not found or propertyname not found on other two lines. It makes sense. How can I achieve this scenario with JsonPatchDocument? or is any other effective way to do this?

Thanks in Advance!

0

There are 0 answers