예제 #1
0
        /// <summary>
        /// Normally we automatically generate this log in DbContext.SaveChanges  function.
        /// However, Identity Framework delete functions does not invoke our custom SaveChanges
        /// method.
        /// </summary>
        /// <param name="operation">Name of operation DELETE/UPDATE ..etc</param>
        /// <param name="entity">Entity to be logged</param>
        /// <returns>RecordAuditLog object to be inserted.</returns>
        private RecordAuditLog GenerateAuditLog(string operation, object entity)
        {
            RecordAuditLog log = new RecordAuditLog();

            log.EntityName = entity.GetType().Name;
            log.Operation  = operation;
            log.Content    = this._xmlSerializer.SerializeToXML(entity);
            log.CreatedBy  = this._contextProvider.UserName;
            log.CreatedOn  = DateTime.UtcNow;

            return(log);
        }
예제 #2
0
        public override int SaveChanges()
        {
            List <RecordAuditLog> auditLogs = new List <RecordAuditLog>();

            foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
            {
                ITrackableEntity trackableEntity = entry.Entity as ITrackableEntity;
                IEntity          entity          = entry.Entity as IEntity;

                if (entry.State == EntityState.Deleted && !(entry.Entity is ISafeToDeleteEntity))
                {
                    RecordAuditLog log = new RecordAuditLog();
                    log.EntityName = entry.Entity.GetType().Name;
                    log.Operation  = entry.State.ToString();
                    log.Content    = this._xmlSerializer.SerializeToXML(entry.Entity);
                    log.CreatedBy  = this._contextProvider.UserName;
                    log.CreatedOn  = DateTime.UtcNow;

                    auditLogs.Add(log);
                }
                else if (entry.State == EntityState.Modified && trackableEntity != null)
                {
                    trackableEntity.ModifiedOn = DateTime.UtcNow;
                    trackableEntity.ModifiedBy = this._contextProvider.UserName;
                }
                else if (entry.State == EntityState.Added && entity != null)
                {
                    if (string.IsNullOrEmpty(entity.CreatedBy))
                    {
                        entity.CreatedBy = this._contextProvider.UserName;
                    }
                    if (entity.CreatedOn == default(DateTime))
                    {
                        entity.CreatedOn = DateTime.UtcNow;
                    }
                }
            }
            if (auditLogs.Count > 0 && AuditLoggingEnabled)
            {
                this.Set <RecordAuditLog>().AddRange(auditLogs);
            }
            try
            {
                return(base.SaveChanges());
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }