I'm creating an audit trail using EF Core's change tracker, and for every entity that has the state Added
, Modified
or Deleted
, I create a new audit entity to insert into my Audit
table. I want to take a snapshot of what I had before an UPDATE
and capture it in JSON and do the same for a snapshot of the entity after the UPDATE
.
var entityType = this.Model.FindEntityType(dbEntry.Entity.GetType());
var originalObjectProperties = new Dictionary<string, object>();
var currentObjectProperties = new Dictionary<string, object>();
foreach (var property in entityType.GetProperties())
{
originalObjectProperties.Add(property.GetColumnName().ToUpper(), dbEntry.OriginalValues[property.Name]);
currentObjectProperties.Add(property.GetColumnName().ToUpper(), dbEntry.CurrentValues[property.Name]);
}
dbEntry
is the entity entry from the change tracker. These give me the same exact JSON though. In my tests I have an INSERT
, UPDATE
and DELETE
, and this is what ends up in my BEFORE_JSON
and AFTER_JSON
in my database for my UPDATE
:
{
"WORK_REQUEST_KEY": 2,
"PROJECT_NAME": "This has now been updated",
"WR_TYPE_KEY": 2
}
{
"WORK_REQUEST_KEY": 2,
"PROJECT_NAME": "This has now been updated",
"WR_TYPE_KEY": 2
}