public HttpResponseMessage PutEmp(int id, EmployeeUpdate employee) { if (ModelState.IsValid && id == employee.EmployeeId) { // Attempt to update the item var updatedEmployee = r.UpdateEmployee(employee); return (updatedEmployee == null) ? Request.CreateResponse(HttpStatusCode.BadRequest) : Request.CreateResponse(HttpStatusCode.OK, updatedEmployee); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// Update specific employee public EmployeeUpdate UpdateEmployee(EmployeeUpdate updatedEmployee) { var e = ds.Employees.Find(updatedEmployee.EmployeeId); if (e == null) { return null; } else { // For the object fetched from the data store, // set its values to those provided // (the method ignores missing properties, and navigation properties) ds.Entry(e).CurrentValues.SetValues(updatedEmployee); ds.SaveChanges(); return updatedEmployee; } }