Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New value / Old value not working exactly. What am I missing? #345

Closed
adeelansari opened this issue Oct 27, 2020 · 2 comments
Closed

New value / Old value not working exactly. What am I missing? #345

adeelansari opened this issue Oct 27, 2020 · 2 comments
Assignees

Comments

@adeelansari
Copy link

Audit.net is not capturing old value and new value when saving into the DB. What am I missing?

{
"EventType": "RepositoryContext",
"Environment": {
"UserName": "myuser",
"MachineName": "DESKTOP",
"DomainName": "DESKTOP",
"CallingMethodName": "PWA.Repository.Repositories.RepositoryWrapper.Save()",
"AssemblyName": "PWA.Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"Culture": "en-US"
},
"StartDate": "2020-10-23T09:42:07.1808807Z",
"EndDate": "2020-10-23T09:42:07.2860043Z",
"Duration": 105,
"EntityFrameworkEvent": {
"Database": "myDB",
"Entries": [
{
"Table": "UserAdditionalInfos",
"Action": "Update",
"PrimaryKey": {
"UserAdditionalInfoId": 3
},
"Changes": [
{
"ColumnName": "AccountStatusId",
"OriginalValue": 1,
"NewValue": 1
},
{
"ColumnName": "CreatedDate",
"OriginalValue": "0001-01-01T00:00:00",
"NewValue": "0001-01-01T00:00:00"
},
{
"ColumnName": "FirstName",
"OriginalValue": "MyFirstName",
"NewValue": "MyFirstName"
},
{
"ColumnName": "LastName",
"OriginalValue": "MyLastName",
"NewValue": "MyLastName"
},
{
"ColumnName": "UserId",
"OriginalValue": "5e0df94d-103e-4136-b5e0-428e853ee62f",
"NewValue": "5e0df94d-103e-4136-b5e0-428e853ee62f"
},
{
"ColumnName": "UserRoleId",
"OriginalValue": "1",
"NewValue": "1"
}
],
"ColumnValues": {
"UserAdditionalInfoId": 3,
"AccountStatusId": 1,
"CreatedDate": "0001-01-01T00:00:00",
"FirstName": "MyFirstName",
"LastName": "MyLastName",
"UserId": "5e0df94d-103e-4136-b5e0-428e853ee62f",
"UserRoleId": "1"
},
"Valid": true
}
],
"Result": 1,
"Success": true
}
}

@thepirat000
Copy link
Owner

Check #53 and #167

@thepirat000 thepirat000 self-assigned this Feb 19, 2021
thepirat000 added a commit that referenced this issue Jul 6, 2023
…tting to query the original values of the entities from the database explicitly to properly set the Original values when using DbContext's `Update` or `Remove` methods. (#53, #167, #291, #335, #345, #606, #607)
@thepirat000
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants