Ejemplo n.º 1
0
        public override int SaveChanges()
        {
            var auditDate   = DateTime.UtcNow;
            var userContext = _userContextProvider.GetUserContext();

            foreach (var entry in ChangeTracker.Entries <IAuditable>().Where(e => e.State == EntityState.Modified || e.State == EntityState.Added))
            {
                switch (entry.State)
                {
                case EntityState.Added:
                    entry.Entity.CreatedOn  = auditDate;
                    entry.Entity.ModifiedOn = auditDate;
                    entry.Entity.CreatedBy  = userContext.UserId;
                    entry.Entity.ModifiedBy = userContext.UserId;
                    break;

                case EntityState.Deleted:
                    break;

                case EntityState.Modified:
                    entry.Property(w => w.CreatedOn).IsModified = false;
                    entry.Property(w => w.CreatedBy).IsModified = false;
                    entry.Entity.ModifiedOn = auditDate;
                    entry.Entity.ModifiedBy = userContext.UserId;
                    break;
                }
            }
            return(base.SaveChanges());
        }