Is there any efficient way to update entity while tracking old and new values for Audit purpose?
var entry = _context.ModelA.Attach(input); var updated = entry.CurrentValues.Clone(); entry.Reload(); entry.CurrentValues.SetValues(updated); entry.State = EntityState.Modified; await _context.SaveChangesAsync();
I have been using code above when updating entity, it is working well but it is not reusable, so I have to duplicate it in every class if needed.
I have also tried the code below, but it is not good when the model has a lot of columns, I have to map each column.
var entry = _context.ModelA.Find(input.Id); entry.ColA = input.ColA; entry.ColB = input.ColB; entry.ColC = input.ColC; _context.Update(entry); await _context.SaveChangesAsync();
Please let me know if there is a better solution for this, note that it will need to track both old and new values for Audit purpose, preferably Reusable method, thanks!