public ChangeEntry(ObjectStateEntry f) { Entity = f.Entity; State = f.State; OriginalValues = new Dictionary<string, object>(); EntityType = Entity.GetType().GetEntityType(); if (State != EntityState.Modified) return; var d = OriginalValues; foreach (var item in f.GetModifiedProperties()) { d[item] = f.OriginalValues[item]; } OriginalValues = d; }
private void AuditChange(ObjectStateEntry entry) { var obj = entry.Entity as IModelObject; if (obj == null) return; var audit = new AuditLog { Action = entry.State.ToString(), Changed = DateTime.Now, ObjectId = obj.Id, User = Thread.CurrentPrincipal.Identity.Name, Collection = entry.EntitySet.Name }; switch (entry.State) { case EntityState.Added: audit.Comment = obj.GetReportHtml(); break; case EntityState.Modified: string report = string.Format("<b>{0}</b><br/>", entry.Entity); foreach (var prop in entry.GetModifiedProperties()) { var displayFormat = entry.Entity.GetType().GetProperty(prop).GetCustomAttribute<DisplayFormatAttribute>(); string format = displayFormat == null ? "{0}" : displayFormat.DataFormatString; report += string.Format( "{0}: {1} => {2}<br/>", prop, string.Format(format, entry.OriginalValues[prop]), string.Format(format, entry.CurrentValues[prop])); } audit.Comment = report; break; case EntityState.Deleted: object original; this.ComparisonContext.TryGetObjectByKey(entry.EntityKey, out original); audit.Comment = ((IModelObject)original).GetReportHtml(); break; default: throw new NotImplementedException("Unhandled state" + entry.State.ToString()); } this.AuditLog.Add(audit); }