Esempio n. 1
0
        /// <summary>
        /// Adds entries to the audit models and returns the RecentUpdateDb. As SaveChanges call is required to commit the changes from the caller.
        /// </summary>
        /// <param name="changeTracker"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public AuditModelContext AuditChanges(DbChangeTracker changeTracker, string userId)
        {
            //Record changes into the Audit DB.
            AuditModelContext recentUpdateDb = new AuditModelContext();

            //Create an exclusion list for audit
            string[] excludedFromAudit = new string[] { "ModifiedById", "Modified", "CreatedById", "Created" };

            IEnumerable <DbEntityEntry> auditEntries = changeTracker.Entries().Where(x => x.Entity is BaseBaseModel && (x.State == EntityState.Modified));


            foreach (DbEntityEntry entry in auditEntries)
            {
                if (entry.GetType().GetCustomAttribute(typeof(NoAuditAttribute)) == null)
                {
                    var currentValues  = entry.CurrentValues;
                    var databaseValues = entry.GetDatabaseValues();

                    if (currentValues != null)
                    {
                        foreach (string propertyName in currentValues.PropertyNames.Except(excludedFromAudit))
                        {
                            if (!string.IsNullOrWhiteSpace(propertyName))
                            {
                                var propertyValue = databaseValues.GetValue <object>(propertyName);

                                //Use nullPlaceholder for equals Operation.
                                object originalValue = propertyValue;
                                object currentValue  = currentValues[propertyName];

                                //Audit changes and non-ReadOnly Fields.
                                if (entry.Entity.GetType().GetProperty(propertyName).GetCustomAttribute(typeof(TFOF.Areas.Core.Attributes.ReadOnlyAttribute)) == null)
                                {
                                    recentUpdateDb.Add(
                                        ObjectContext.GetObjectType(entry.Entity.GetType()).Name,
                                        ((BaseBaseModel)entry.Entity).GetPrimaryKey(),
                                        propertyName,
                                        originalValue,
                                        currentValue,
                                        userId
                                        );
                                }
                            }
                        }
                    }
                }
            }
            return(recentUpdateDb);
        }
Esempio n. 2
0
        public int SaveChanges(string userId)
        {
            //Update Modified and ModifiedById For BaseModel classes
            IEnumerable <DbEntityEntry> entries = ChangeTracker.Entries().Where(x => x.Entity is BaseModel && (x.State == EntityState.Added || x.State == EntityState.Modified));

            foreach (DbEntityEntry entry in entries)
            {
                DateTime datetime = DateTime.Now;
                ((BaseModel)entry.Entity).Modified     = datetime;
                ((BaseModel)entry.Entity).ModifiedById = userId;

                if (entry.State == EntityState.Added)
                {
                    WellTableModel wellTableModel = ((BaseModel)entry.Entity).WellTable;
                    if (wellTableModel != null && entry.Property(wellTableModel.IdField).CurrentValue == null)
                    {
                        entry.Property(wellTableModel.IdField).CurrentValue = ((BaseModel)entry.Entity).GetWellId();
                    }
                    ((BaseModel)entry.Entity).Created     = datetime;
                    ((BaseModel)entry.Entity).CreatedById = userId;
                }
                else
                {
                    entry.Property("Created").IsModified     = false;
                    entry.Property("CreatedById").IsModified = false;
                }
            }

            //Update Id for BaseBaseModel classes
            IEnumerable <DbEntityEntry> idEntries = ChangeTracker.Entries().Where(x => x.Entity is BaseBaseModel && (x.State == EntityState.Added));

            foreach (DbEntityEntry entry in idEntries)
            {
                WellTableModel wellTableModel = ((BaseBaseModel)entry.Entity).WellTable;
                if (wellTableModel != null && entry.Property(wellTableModel.IdField).CurrentValue == null)
                {
                    entry.Property(wellTableModel.IdField).CurrentValue = ((BaseBaseModel)entry.Entity).GetWellId();
                }
            }

            //Set IsModified = false for readonly Properties
            IEnumerable <DbEntityEntry> readOnlyEntries = ChangeTracker.Entries().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified);

            foreach (DbEntityEntry entry in readOnlyEntries)
            {
                var currentValues = entry.CurrentValues;
                if (currentValues != null)
                {
                    foreach (string propertyName in currentValues.PropertyNames)
                    {
                        if (entry.Entity.GetType().GetProperty(propertyName).GetCustomAttribute(typeof(TFOF.Areas.Core.Attributes.ReadOnlyAttribute)) != null)
                        {
                            entry.Property(propertyName).IsModified = false;
                        }
                    }
                }
            }

            //Record Changes
            AuditModelContext recentUpdateDb = AuditChanges(ChangeTracker, userId);

            //Records need to be saved first before audit
            try
            {
                int affected = base.SaveChanges();
                recentUpdateDb.SaveChanges(userId);
                return(affected);
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var entryError in ex.EntityValidationErrors)
                {
                    foreach (var error in entryError.ValidationErrors)
                    {
                        throw new DbEntityValidationException(
                                  "Entity Validation Failed for " + error.PropertyName + ": " +
                                  error.ErrorMessage
                                  );               // Add the original exception as the inn
                    }
                }
            }
            return(0);
        }