Original value does not report previous value during update
See original GitHub issueDescribe the bug Original value does not report previous value during update. Basically OriginalValue has the same value as NewValue.
To Reproduce
Supplied my own Audit Model and configured using the following code
Audit.Core.Configuration.Setup() .UseEntityFramework(_ => _ .AuditTypeMapper(t => typeof(IO.Database.Models.Audition.Audit)) .AuditEntityAction<IO.Database.Models.Audition.Audit>((ev, entry, entity) => { entity.AuditId = Guid.NewGuid(); entity.DateTime = DateTime.Now; entity.TableName = entry.EntityType.Name; entity.KeyValues = JsonSerializer.Serialize(entry.PrimaryKey); entity.NewValues = entry.ToJson(); entity.Operation = (Operation)Enum.Parse(typeof(Operation), entry.Action); //entity.AuditData = entry.ToJson(); //entity.EntityType = entry.EntityType.Name; //entity.AuditDate = DateTime.Now; //entity.TablePk = entry.PrimaryKey.First().Value.ToString(); entity.PerformedBy = GetUserFromContext(contextAccessor); }) .IgnoreMatchedProperties(true));
Everything works as expected except for what I store in “NewValues” during an update. The OriginalValue is reporting the same value as NewValue. So if what I add is “Test1” and I update field to “Test2”, both OriginalValue and NewValue will report “Test2” which is wrong.
Expected behavior OriginalValue should display the previous value stored in the database during an update and not the new value.
Libraries (specify the Audit.NET extensions being used including version):
- Audit.EntityFramework.Core : 16.1.3
Target .NET framework: For example:
- .NET Standard 2.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
From the posts you sent, I was able to modify my repository library by doing this
`
`
which is of course followed by a save. It is a lot more code that is needed as opposed to what I had before which was :
`
context.UpdateRange(entities); await context.SaveChangesAsync().ConfigureAwait(false)
` It is a bit slower now but at least it works. Of course, if I have multiple entities, I have to iterate through each of them so I presume this will translate at a cost in speed but hey, it is what it is.
Thanks for the help.
Starting from version 21.0.2, the Audit.EntityFramework and Audit.EntityFramework.Core libraries introduce a new feature.
The setting called
ReloadDatabaseValues
can be globally or individually configured for a DbContext instance. It determines whether the original values of audited entities should be fetched from the database prior to saving the audit event.Consider the following examples of update and delete operations:
If the ReloadDatabaseValues is not set to true, the EF Change Tracker will lack knowledge of the original values.
Enabling the
ReloadDatabaseValues
setting triggers an extra database query to retrieve the original values prior to the update operation. As a result, the audit event will contain the original values.