7

I have a issue in EF7/asp.Net Core application. In my context I create a method Save:

    public int Save()
    {
        ChangeTracker.DetectChanges();
        var modifiedEntities = ChangeTracker.Entries()
            .Where(p => p.State == EntityState.Modified || p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified || p.State == EntityState.Detached).ToList();
        var now = DateTime.UtcNow;

        foreach (var change in modifiedEntities)
        {
            var entityName = change.Entity.GetType().Name;
            var primaryKeyValue = GetPrimaryKeyValue(change.Entity);
            foreach (var prop in change.Entity.GetType().GetTypeInfo().DeclaredProperties)
            {
                if (!prop.GetGetMethod().IsVirtual)
                {
                    var currentValue = change.Property(prop.Name).CurrentValue;
                    var originalValue = change.Property(prop.Name).OriginalValue;
                    if (originalValue.ToString() != currentValue.ToString())
                    {
                        var changeLoged = new ChangeLog
                        {
                            PropertyName = prop.Name,
                            EntityName = entityName,
                            PrimaryKeyValue = primaryKeyValue,
                            DateChange = now,
                            OldValue = originalValue.ToString(),
                            NewValue = currentValue.ToString(),
                            ChangedBy = "test"
                        };
                        ChangeLog.Add(changeLoged);
                    }
                }
            }
        }
        return base.SaveChanges();
    }

and method GetPrimaryKeyValue:

protected virtual int GetPrimaryKeyValue<T>(T entity)
    {
        var test = entity;
        var test2 = test.GetType();
        var keyName = this.Model.FindEntityType(test2).FindPrimaryKey().Properties
            .Select(x => x.Name).Single();
        var result = (int)entity.GetType().GetProperty(keyName).GetValue(entity, null);
        if (result < 0)
            return -1;

        return result;
    }

Unfortunatlly the change.Property(prop.Name).CurrentValue always equals OriginalValue, so the if originalValue.ToString() != currentValue.ToString()

always return false.

2
  • Take a look at Audit.EntityFramework library. It intercepts the SaveChanges of your context to generate detailed Audit Logs for EF CRUD operations. Commented Sep 8, 2016 at 14:26
  • This being tagged with both entity-framework and entity-framework-core is sort of confusing, as they are divergent frameworks. Commented Feb 16, 2021 at 21:29

3 Answers 3

14

Replace:

var originalValue = change.Property(prop.Name).OriginalValue;

to:

var originalValue = change.GetDatabaseValues().GetValue<object>(prop.Name);
1
  • 1
    Thanks, this was what I needed. I wonder now if there will be extra round trips to the database? So I first got the DB values before I start looping over the properties: var entsInDB = Entry.GetDatabaseValues(); foreach (PropertyEntry property in Entry.Properties) {
    – Mark
    Commented Aug 24, 2022 at 8:28
3

This will not exactly answer your question since I cannot reproduce this issue but this may help you.

In EF Core, the PropertyEntry class has now an IsModified property which let you know if the value has been modified or not.

You should use it instead:

if (change.Property(prop.Name).IsModified)
{
    var changeLoged = new ChangeLog
    {
        PropertyName = prop.Name,
        EntityName = entityName,
        PrimaryKeyValue = primaryKeyValue,
        DateChange = now,
        OldValue = originalValue.ToString(),
        NewValue = currentValue.ToString(),
        ChangedBy = "test"
    };
    ChangeLog.Add(changeLoged);
}

Disclaimer: I'm the owner of the project Entity Framework Plus

The Library has an Audit Feature (Supporting EF Core) which you may use or get inspired by to create your auditing (The code is Open Source).

Documentation: EF+ Audit

1
  • Does it get set to modified if it's different from the original, or if it has been written to? Commented Feb 16, 2021 at 21:30
3

Based on the answers above I refactored my SetChanges method to this, and its working fine now! (EF 6 and .NET 6)

private void SetChanges()
    {
        TableName = Entry.Metadata.GetTableName();

        var entsInDB = Entry.GetDatabaseValues();

        foreach (PropertyEntry property in Entry.Properties)
        {
            if (property != null)
            {
                string propertyName = property.Metadata.Name;

                if (property.Metadata.IsPrimaryKey())
                {
                    KeyValues[propertyName] = property.CurrentValue ?? "";
                    continue;
                }

                switch (Entry.State)
                {
                    case EntityState.Added:
                        NewValues[propertyName] = property.CurrentValue ?? "";
                        AuditType = AuditType.Create;
                        break;

                    case EntityState.Deleted:
                        OldValues[propertyName] = property.OriginalValue ?? "";
                        AuditType = AuditType.Delete;
                        break;

                    case EntityState.Modified:
                        if (property.IsModified)
                        {
                            var originalValue = entsInDB?.GetValue<object>(property.Metadata.Name);
                            if (originalValue?.ToString() != property.CurrentValue?.ToString())
                            {
                                ChangedColumns.Add(propertyName);
                                OldValues[propertyName] = originalValue?.ToString() ?? "";
                                NewValues[propertyName] = property.CurrentValue ?? "";
                                AuditType = AuditType.Update;
                            }
                        }
                        break;
                }
            }
        }
    }

Logging it to the database audit table: Audit logs

1
  • worked. than you very much
    – Venkat
    Commented Oct 21, 2022 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.