public static void DeleteAuditLog(this IQueryable <AuditLog> auditLogs, AuditLog auditLogToDelete)
 {
     DeleteAuditLog(auditLogs, new List <AuditLog> {
         auditLogToDelete
     });
 }
예제 #2
0
        // private so I didn't have to write the warning above twice.

        private int SaveChangesImpl(Person person, TransactionScope scope)
        {
            // We have Configuration.AutoDetectChangesEnabled turned off by default instead of it being true out of the box
            // We only need to detect changes when we know we are saving
            ChangeTracker.DetectChanges();

            var dbEntityEntries = ChangeTracker.Entries().ToList();
            var addedEntries    = dbEntityEntries.Where(e => e.State == EntityState.Added).ToList();
            var modifiedEntries = dbEntityEntries.Where(e => e.State == EntityState.Deleted || e.State == EntityState.Modified).ToList();
            var objectContext   = GetObjectContext();

            foreach (var entry in modifiedEntries)
            {
                // For each changed record, get the audit record entries and add them
                var auditRecordsForChange = AuditLog.GetAuditLogRecordsForModifiedOrDeleted(entry, person, objectContext);
                AuditLogs.AddRange(auditRecordsForChange);
            }

            int changes;

            try
            {
                changes = base.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                var sb = new StringBuilder();

                foreach (var failure in ex.EntityValidationErrors)
                {
                    sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                    foreach (var error in failure.ValidationErrors)
                    {
                        sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                        sb.AppendLine();
                    }
                }

                throw new DbEntityValidationException(
                          "Entity Validation Failed - errors follow:\n" +
                          sb.ToString(), ex
                          ); // Add the original exception as the innerException
            }

            foreach (var entry in addedEntries)
            {
                // For each added record, get the audit record entries and add them
                var auditRecordsForChange = AuditLog.GetAuditLogRecordsForAdded(entry, person, objectContext);
                AuditLogs.AddRange(auditRecordsForChange);
            }
            // we need to save the audit log entries now
            try
            {
                base.SaveChanges();
            }
            catch (DbEntityValidationException validationException)
            {
                Console.WriteLine(validationException);
                throw;
            }

            scope.Complete();
            return(changes);
        }
예제 #3
0
 /// <summary>
 /// This is for adding any extra optional columns like ProjectID to the audit log record
 /// </summary>
 /// <param name="entry"></param>
 /// <param name="tableName"></param>
 /// <param name="auditLog"></param>
 private static void AddAdditionalRecordColumns(DbEntityEntry entry, string tableName, AuditLog auditLog)
 {
     if (Project.DependentEntityTypeNames.Contains(tableName))
     {
         var projectID = (int?)entry.Property(PropertyNameProjectID).CurrentValue;
         if (projectID.HasValue)
         {
             auditLog.ProjectID = projectID;
         }
     }
 }