public override int SaveChanges() { ObjectContext context = ((IObjectContextAdapter)this).ObjectContext; var now = DateTime.Now; var modifiedEntities = ChangeTracker.Entries() .Where(p => p.State == EntityState.Modified).ToList(); foreach (var change in modifiedEntities) { var entityName = change.Entity.GetType().BaseType.Name; var primaryKey = GetPrimaryKeyValue(change); foreach (var prop in change.OriginalValues.PropertyNames) { string originalValue; if (change.OriginalValues[prop] != null) { originalValue = change.OriginalValues[prop].ToString(); } else { originalValue = ""; } string currentValue; if (change.CurrentValues[prop] != null) { currentValue = change.CurrentValues[prop].ToString(); } else { currentValue = ""; } if (originalValue != currentValue) { UN_AuditLog log = new UN_AuditLog() { TableName = entityName, PrimaryKeyID = (int)primaryKey, ColumnName = prop, OldValue = originalValue, NewValue = currentValue, DateChanged = now, id_userLogin = Settings.id_userLogin, Operation = "Modify" }; this.UN_AuditLog.Add(log); } } } IEnumerable <ObjectStateEntry> objectStateEntries = from e in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified) where e.IsRelationship == false && e.Entity != null select e; var currentTime = DateTime.Now; foreach (var entry in objectStateEntries) { dynamic dv = entry.Entity; dv.Timestamp = now; dv.id_userLogin = Settings.id_userLogin; } return(base.SaveChanges()); }
public override int SaveChanges() { ObjectContext context = ((IObjectContextAdapter)this).ObjectContext; var now = DateTime.Now; var modifiedEntities = ChangeTracker.Entries() .Where(p => p.State == EntityState.Modified).ToList(); foreach (var change in modifiedEntities) { var entityName = change.Entity.GetType().BaseType.Name; var primaryKey = GetPrimaryKeyValue(change); foreach (var prop in change.OriginalValues.PropertyNames) { string originalValue; if (change.OriginalValues[prop] != null) { originalValue = change.OriginalValues[prop].ToString(); } else { originalValue = ""; } string currentValue; if(change.CurrentValues[prop] != null) { currentValue = change.CurrentValues[prop].ToString(); } else { currentValue = ""; } if (originalValue != currentValue) { UN_AuditLog log = new UN_AuditLog() { TableName = entityName, PrimaryKeyID = (int)primaryKey, ColumnName = prop, OldValue = originalValue, NewValue = currentValue, DateChanged = now, id_userLogin = Settings.id_userLogin, Operation = "Modify" }; this.UN_AuditLog.Add(log); } } } IEnumerable<ObjectStateEntry> objectStateEntries = from e in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified) where e.IsRelationship == false && e.Entity != null select e; var currentTime = DateTime.Now; foreach (var entry in objectStateEntries) { dynamic dv = entry.Entity; dv.Timestamp = now; dv.id_userLogin = Settings.id_userLogin; } return base.SaveChanges(); }