示例#1
0
        public override int SaveChanges()
        {
            var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

            var currentUsername = "******";
            if (UserNameEvent != null)
            {
                var e = new UserNameEventArgs();
                UserNameEvent(this, e);
                currentUsername = e.UserName;
            }

            //var currentUsername = HttpContext.Current != null && HttpContext.Current.User != null
            //    ? HttpContext.Current.User.Identity.Name
            //    : "Anonymous";

            foreach (var entity in entities)
            {
                if (entity.Entity is BaseEntity)
                {
                    if (entity.State == EntityState.Added)
                    {
                        ((BaseEntity)entity.Entity).CreatedDate = DateTime.Now;
                        ((BaseEntity)entity.Entity).CreatedBy = currentUsername;
                    }

                    ((BaseEntity)entity.Entity).ModifiedDate = DateTime.Now;
                    ((BaseEntity)entity.Entity).ModifiedBy = currentUsername;
                }
            }

            return base.SaveChanges();
        }
示例#2
0
        public override int SaveChanges()
        {
            var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

            var currentUsername = "******";

            if (UserNameEvent != null)
            {
                var e = new UserNameEventArgs();
                UserNameEvent(this, e);
                currentUsername = e.UserName;
            }

            //var currentUsername = HttpContext.Current != null && HttpContext.Current.User != null
            //    ? HttpContext.Current.User.Identity.Name
            //    : "Anonymous";

            foreach (var entity in entities)
            {
                if (entity.Entity is BaseEntity)
                {
                    if (entity.State == EntityState.Added)
                    {
                        ((BaseEntity)entity.Entity).CreatedDate = DateTime.Now;
                        ((BaseEntity)entity.Entity).CreatedBy   = currentUsername;
                    }

                    ((BaseEntity)entity.Entity).ModifiedDate = DateTime.Now;
                    ((BaseEntity)entity.Entity).ModifiedBy   = currentUsername;
                }
            }

            return(base.SaveChanges());
        }