question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Original value does not report previous value during update

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tessierpcommented, Sep 22, 2020

From the posts you sent, I was able to modify my repository library by doing this

`

            var set = context.Set<TEntity>();
            var entry = set.Attach(entity);
            var updated = entry.CurrentValues.Clone();
            await entry.ReloadAsync();
            entry.CurrentValues.SetValues(updated);
            entry.State = EntityState.Modified;

`

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.

0reactions
thepirat000commented, Jul 6, 2023

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:

using (var context = new MyAuditedContext())
{
    //context.ReloadDatabaseValues = true;

    context.Cars.Update(new Car() { Id = 123, Name = "New name" });
    await context.SaveChangesAsync();
}
using (var context = new MyAuditedContext())
{
    //context.ReloadDatabaseValues = true;

    context.Entry(new Car() { Id = 123 }).State = EntityState.Deleted;
    await context.SaveChangesAsync();
}

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to retrieve the previous value when updating
As for previous() it returns original or previously saved values before any set calls and until save will be called.
Read more >
Previous Value of Field On Update
I am thinking the only way to do this is to hold the old value in a hidden field and then update the...
Read more >
Trigger after update save new value but revert to previous ...
I Have a sobject name member__c in which there is a field text__c. Text__c has a value called HI. i saved the record....
Read more >
Update previous value field - oracle
I inherited a database that has multiple tables that contain some form of an object ID, a date, and a value. The metrics...
Read more >
How is SQL Server returning both a new value and old ...
I learned that after an UPDATE has started, and data has been modified, it is possible to read the old values. In the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found