Scenario :
Assume a case where we have an Employee object and we have an nullable integer named Age (int? Age). There are two cases we want to achieve,the first case is to not update the Age property and we could achieve this by passing null value to Age and ignoring the Age Property while saving and second case is to reset the Age i.e Accept whatever is passed by Age Property and save it in db.
Solution :
We want to only update properties of Employee object that is being passed to the controller and we call it as “partial update”(PATCH). In this case we update only what is being passed and leave the system unchanged. Microsoft provides OData for partial update.
- Get Microsoft ASP.NET Web API OData package from Nuget.
- In your controller HTTP verb Method,you could make use of Delta<T> to map your Employee object and this will take care of partial update with Patch Method provided by ODATA .
public ApiResponse<Statuscode> Patch(Delta<Employee>; employee) { var employeebyId == dbcontext.FirstorDefault(x=>x.EmpId ==employee.EmpId); if(employee !=null) { employee.Patch(employeebyId ); // Partial update by ODATA } }