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