public override int SaveChanges() { try { if (ChangeTracker.HasChanges()) { var entries = this.ChangeTracker.Entries(); foreach (DbEntityEntry entry in entries) { if (entry.Entity != null) { if (entry.State == EntityState.Added) { //entry is Added var model = (EntityBase)entry.Entity; model.CreatedBy = DataConnector.LoginName; model.CreatedOn = DateTime.Now; model.UpdatedBy = DataConnector.LoginName; model.UpdatedOn = DateTime.Now; } else if (entry.State == EntityState.Deleted) { //entry in deleted } else { //entry is modified var model = (EntityBase)entry.Entity; model.UpdatedBy = DataConnector.LoginName; model.UpdatedOn = DateTime.Now; } _auditManager.AddAudit(entry, DataConnector.LoginName); } } } _auditManager.Save(); return(base.SaveChanges()); } catch (DbUpdateException e) { var innerEx = e.InnerException; while (innerEx.InnerException != null) { innerEx = innerEx.InnerException; } throw new Exception(innerEx.Message); } catch (DbEntityValidationException e) { var sb = new StringBuilder(); foreach (var entry in e.EntityValidationErrors) { foreach (var error in entry.ValidationErrors) { sb.AppendLine(string.Format("{0}-{1}-{2}", entry.Entry.Entity, error.PropertyName, error.ErrorMessage)); } } throw new Exception(sb.ToString()); } catch (Exception e) { var innerEx = e.InnerException; while (innerEx.InnerException != null) { innerEx = innerEx.InnerException; } throw new Exception(innerEx.Message); } }