jheatherington
jheatherington

Reputation: 55

Entity Framework Audit Trail

I am having issues with my code below. Everything works fine for getting Added and Deleted Entries. I have access to all new data in my modified section, but for some reason I can't get the original values. Any help will be most appreciated

public override int SaveChanges()
    {

        ChangeTracker.DetectChanges(); // Important!

        ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

        List<ObjectStateEntry> objectStateEntryList =
            ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                       | EntityState.Modified
                                                       | EntityState.Deleted)
            .ToList();
        List<Log> LogEntries = new List<Log>();

        foreach (ObjectStateEntry entry in objectStateEntryList)
        {

            if ((!entry.IsRelationship) && (entry.Entity is Scan))
            {
                Log logEntry = new Log();
                JavaScriptSerializer serializer= new JavaScriptSerializer();
                logEntry.TimeStamp = System.DateTime.Now;
                logEntry.Username = "me";
                IEnumerable<string> modifiedProperties = entry.GetModifiedProperties();
                logEntry.ChangedColumns = serializer.Serialize(modifiedProperties);
                logEntry.TableName = entry.EntitySet.Name;

                switch (entry.State)
                {
                    case EntityState.Added:
                        {

                            logEntry.Action = "Added";
                            logEntry.NewValue = serializer.Serialize(entry.Entity);
                            logEntry.OriginalValue = "";

                            // write log...
                            break;
                        }
                    case EntityState.Deleted:
                        {// write log...

                            logEntry.Action = "Deleted";
                            logEntry.NewValue = serializer.Serialize(entry.Entity);
                            logEntry.OriginalValue = "";

                            break;
                        }
                    case EntityState.Modified:
                        {
                            logEntry.NewValue = "{";
                            logEntry.OriginalValue = "{";
                            foreach (string propertyName in
                                         entry.GetModifiedProperties())
                            {
                                DbDataRecord original = entry.OriginalValues;
                                string oldValue = original.GetValue(
                                    original.GetOrdinal(propertyName))
                                    .ToString();

                                CurrentValueRecord current = entry.CurrentValues;
                                string newValue = current.GetValue(
                                    current.GetOrdinal(propertyName))
                                    .ToString();
                                logEntry.NewValue += "\"" + propertyName + "\":\"" + newValue + "\",";
                                logEntry.OriginalValue += "\"" + propertyName + "\":\"" + oldValue + "\",";
                            }
                            logEntry.NewValue = logEntry.NewValue.TrimEnd(',') + "}";
                            logEntry.OriginalValue = logEntry.OriginalValue.TrimEnd(',') + "}";
                            break;
                        }
                }
                LogEntries.Add(logEntry);

            }
        }
        foreach (Log addLog in LogEntries)
        {
            this.Logs.Add(addLog);
        }
        return base.SaveChanges();



    }

Upvotes: 0

Views: 2411

Answers (2)

user3083619
user3083619

Reputation:

You might want to check out the AuditDBContext project on Codeplex. http://auditdbcontext.codeplex.com/

The author stores on update or delete the new values along with the old is saved with other details such as the user who committed the action, timestamp etcetera.

The source code for this project will show you how to get the old values.

Upvotes: 0

Brian Ogden
Brian Ogden

Reputation: 19212

Check out this article, the author mentions that his first approach fails for inserts and he had revamp things a bit.

Upvotes: 1

Related Questions