original and current values the same – change tracking is not working

After following this awesome tutorial, my change tracker is not showing that the values have changed.

Here's my method:

    public override int SaveChanges()
    {
        var modifiedEntities = ChangeTracker.Entries()
            .Where(p => p.State == EntityState.Modified).ToList();
        var now = DateTime.UtcNow;

        foreach (var change in modifiedEntities)
        {
            var entityName = change.Entity.GetType().Name;
            var primaryKey = GetPrimaryKeyValue(change);

            foreach (var prop in change.OriginalValues.PropertyNames)
            {
                var originalValue = change.OriginalValues[prop].ToString();
                var currentValue = change.CurrentValues[prop].ToString();
                if (originalValue != currentValue)
                {
                    ChangeLog log = new ChangeLog()
                    {
                        EntityName = entityName,
                        PrimaryKeyValue = primaryKey.ToString(),
                        PropertyName = prop,
                        OldValue = originalValue,
                        NewValue = currentValue,
                        DateChanged = now
                    };
                    ChangeLogs.Add(log);
                }
            }
        }
        return base.SaveChanges();
    }

Why is the originalValue is the same as the currentValue?

enter image description here

When making a change, for some reason the original and current value remain the same. What am I doing wrong?

The ChangeTracker is showing that 1 record has been changed!

enter image description here



Read more here: https://stackoverflow.com/questions/65585618/original-and-current-values-the-same-change-tracking-is-not-working

Content Attribution

This content was originally published by Alex Gordon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: