3

I have extended the DbContext to enable auditing and I'm facing an issue when I have modifications because of the collection object I'm using to track reports no modifications and also, for a new record, I'm not getting the Primary Key:

public partial class FinanceConnection
{
    public int SaveChanges(int userId)
    {
        RecordAudit(userId);
        // AddTimestamps(userId);

        return base.SaveChanges();
    }

    public Task<int> SaveChangesAsync(int userId)
    {
        RecordAudit(userId);
        // AddTimestamps(userId);

        return base.SaveChangesAsync();
    }

    private void RecordAudit(int userId)
    {
        try
        {
            var listLogs = GetLogEntries(((IObjectContextAdapter)this).ObjectContext.ObjectStateManager, userId);

            foreach (var log in listLogs)
            {
                var auditLog = new AUDIT_LOG
                {
                    ID = log.Id,
                    AUDIT_TYPE_ID = log.Action,
                    TABLE_NAME = log.TableName,
                    COLUMN_NAME = log.ColumnName,
                    RECORD_PK = log.PrimaryKey,
                    OLD_VALUE = log.OldValue,
                    NEW_VALUE = log.NewValue,
                    RECORD_STAMP = log.Date,
                    USER_ID = log.UserId
                };

                AUDIT_LOGS.Add(auditLog);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    private IEnumerable<CustomLog> GetLogEntries(ObjectStateManager entities, int userId)
    {
        var listLogs = new List<CustomLog>();
        var entries = entities.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted);
        var modifiedEntities = this.ChangeTracker.Entries().Where(p => p.State == EntityState.Added || p.State == EntityState.Modified || p.State == EntityState.Deleted).ToList();

        foreach (var entry in entries)
        {
            var tableName = entry.Entity.GetType().Name;
            var pk = GetPrimaryKeys(entry);

            switch (entry.State)
            {
                case EntityState.Added:
                {
                    ...
                    break;
                }
                case EntityState.Modified:
                {
                    ...
                    break;
                }
                case EntityState.Deleted:
                {
                    ...
                    break;
                }
            }
        }

        return listLogs;
    }

    private static string GetPrimaryKeys(ObjectStateEntry entry)
    {
        var pk = string.Empty;
        if (entry.EntityKey == null || entry.EntityKey.EntityKeyValues == null || entry.EntityKey.EntityKeyValues.Length == 0) return "N/A";
        return entry.EntityKey.EntityKeyValues.Aggregate(pk, (current, keyValue) => current + $"{keyValue.Key}={keyValue.Value};");
    }

}

Issues: - I cannot get the primary key for the new record. New record case works. - I cannot get the modified case as the entities.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted) returns 0 for modified records

DoomerDGR8
  • 4,840
  • 6
  • 43
  • 91

0 Answers0