Пример #1
0
        private TblAuditoria RegistrosAuditoria(DbEntityEntry entry, string UserName)
        {
            TblAuditoria audit = new TblAuditoria();

            //audit.ID_AUDIT = Guid.NewGuid().ToString();
            audit._FechaCambio = DateTime.Now;
            // Obtiene el atributo Table() si existe
            TableAttribute tableAttr = entry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;

            // Obtiene el nombre de la table en el contexto (Si tiene un nombre en el atributo tabla, lo usa, sino toma el nombre de la clase)
            audit._NombreTabla = tableAttr != null ? tableAttr.Name : entry.Entity.GetType().Name;
            audit._UserID      = UserName;

            if (entry.State == EntityState.Added)
            {//entrada es un nuevo registro
                audit._ValorActual    = GetValueToXml(entry, false);
                audit._ValorAnterior  = null;
                audit._TipoEvento     = "Agregar";
                audit._NombreColumnas = "TODAS";
            }
            else if (entry.State == EntityState.Deleted)
            {//entrada fue eliminada
                audit._ValorAnterior  = GetValueToXml(entry, true);
                audit._ValorActual    = null;
                audit._TipoEvento     = "Eliminar";
                audit._NombreColumnas = "TODAS";
            }
            else
            {//entrada fue modificada
                audit._ValorAnterior = GetValueToXml(entry, true);
                audit._ValorActual   = GetValueToXml(entry, false);
                audit._TipoEvento    = "Modificar";

                foreach (string propertyName in entry.OriginalValues.PropertyNames)
                {
                    // Para modificación, tomamos solo las columnas que han sido modificadas.
                    if (!object.Equals(entry.OriginalValues.GetValue <object>(propertyName), entry.CurrentValues.GetValue <object>(propertyName)))
                    {
                        audit._NombreColumnas = (audit._NombreColumnas == null) ? propertyName : audit._NombreColumnas + "," + propertyName;
                    }
                }
            }

            return(audit);
        }
Пример #2
0
        public int SaveChanges(string userId)
        {
            // Obtiene todos los cambios que se hicieron es un contexto
            foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == System.Data.Entity.EntityState.Added || p.State == System.Data.Entity.EntityState.Deleted || p.State == System.Data.Entity.EntityState.Modified))
            {
                Type type        = ent.Entity.GetType();
                var  dnAttribute = type.GetCustomAttributes(
                    typeof(AuditoriaAttribute), true
                    ).FirstOrDefault() as AuditoriaAttribute;
                if (dnAttribute != null)
                {
                    // Por cada cambio de un registro, obtiene los valores para insertar en Auditoría.
                    TblAuditoria x = new TblAuditoria();
                    x = RegistrosAuditoria(ent, userId);
                    this.Auditoria.Add(x);
                }
            }

            // Devuelve el método SaveChange() original de EF, que guardará los cambios principales con la auditoria
            return(base.SaveChanges());
        }