You can also solve this by setting ConflictDetection="CompareAllValues"
in the ObjectDataSource
:
<asp:ObjectDataSource ID="objDsEmployees"
runat="server"
DeleteMethod="Delete"
InsertMethod="Insert"
SelectMethod="GetAll"
UpdateMethod="Update"
DataObjectTypeName="App.Core.Domain.Employee"
TypeName="App.Core.Services.EmployeeService"
OldValuesParameterFormatString="original{0}"
ConflictDetection="CompareAllValues" />
Then in the Update
method in EmployeeService
we fetch the original employee first by its id, then we merge this original employee with the updated employee. This makes sure that not updated properties don't become null - although it requires an extra call to the database:
public void Update(Employee originalEmployee, Employee employee)
{
// Get the original employee by its id.
Employee fullOriginalEmployee = GetById(originalEmployee.EmployeeId);
// Merge the data of the updated employee with the original employee.
fullOriginalEmployee.Name = employee.Name;
fullOriginalEmployee.Email = employee.Email;
fullOriginalEmployee.Telephone = employee.Telephone;
fullOriginalEmployee.BirthDate = employee.BirthDate;
// Persist changes in database.
SaveChanges();
}