public int UpdatePartial(int id, dynamic customer, CustomerDb db) { if (IsDbContextNull(db)) { return(0); } Customer trackedCustomer = null; try { trackedCustomer = db.Customers.SingleOrDefault(c => c.Id == id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while getting customer by id{(_logSensitiveData ? "{" + id + "}" : "")} for partial update"); } if (trackedCustomer == null) { _logger.LogDebug($"No customer found for given id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } if (customer.GetType().GetProperty("Id") != null) { customer.Id = id; //cannot update the id } try { db.Entry(trackedCustomer).CurrentValues.SetValues(customer); return(db.SaveChanges()); } catch (Exception e) { _logger.LogError(e, $"Error occurred while partially updating customer{(_logSensitiveData ? JsonConvert.SerializeObject(customer, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }) : " ")}"); return(0); } }
public int UpdateFull(int id, Customer customer, CustomerDb db) { if (IsDbContextNull(db) || IsCustomerNull(customer)) { return(0); } Customer trackedCustomer = null; try { trackedCustomer = db.Customers.SingleOrDefault(c => c.Id == id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while getting customer by id{(_logSensitiveData ? "{" + id + "}" : "")} for full update"); return(0); } if (trackedCustomer == null) { _logger.LogDebug($"No customer found for given id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } customer.Id = id; //avoid updating the id try { db.Entry(trackedCustomer).CurrentValues.SetValues(customer); return(db.SaveChanges()); } catch (Exception e) { _logger.LogError(e, $"Error occurred while fully updating customer{(_logSensitiveData ? JsonConvert.SerializeObject(customer) : "")}"); return(0); } }