예제 #1
0
        /// <summary>
        /// 检查最后修改时间
        /// </summary>
        /// <typeparam name="TUserKey"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public TEntity CheckIModificationAudited <TUserKey>(TEntity entity)
            where TUserKey : struct, IEquatable <TUserKey>
        {
            if (!entity.GetType().IsBaseOn(typeof(IModificationAudited <>)))
            {
                return(entity);
            }

            IModificationAudited <TUserKey> entity1 = (IModificationAudited <TUserKey>)entity;

            entity1.LastModifierUserId = _principal?.Identity?.GetUesrId <TUserKey>();
            entity1.LastModifionTime   = DateTime.Now;
            return((TEntity)entity1);
        }
예제 #2
0
 public static void SetModificationAuditProperties(object entityAsObj, long?userId)
 {
     if (entityAsObj is IHasModificationTime)
     {
         Extensions.As <IHasModificationTime>(entityAsObj).LastModificationTime = DateTime.Now;
     }
     if (entityAsObj is IModificationAudited)
     {
         IModificationAudited modificationAudited = Extensions.As <IModificationAudited>(entityAsObj);
         if (!userId.HasValue)
         {
             modificationAudited.LastModifierUserId = null;
         }
         else
         {
             modificationAudited.LastModifierUserId = userId;
         }
     }
 }
예제 #3
0
        protected virtual void SetModificationAuditProperties(DbEntityEntry entry, long?userId)
        {
            if (entry.Entity is IHasModificationTime)
            {
                entry.Cast <IHasModificationTime>().Entity.LastModificationTime = Clock.Now;
            }

            if (entry.Entity is IModificationAudited)
            {
                IModificationAudited entity = entry.Cast <IModificationAudited>().Entity;

                if (userId == null)
                {
                    entity.LastModifierUserId = null;
                    return;
                }

                entity.LastModifierUserId = userId;
            }
        }
        public static void SetModificationAuditProperties(object entityAsObj, int?userId)
        {
            if (entityAsObj is IHasModificationTime)
            {
                entityAsObj.As <IHasModificationTime>().LastUpdatedDate = new DateTime?(DateTime.Now);
            }
            if (!(entityAsObj is IModificationAudited))
            {
                return;
            }
            IModificationAudited modificationAudited = entityAsObj.As <IModificationAudited>();

            if (!userId.HasValue)
            {
                modificationAudited.LastUpdatedUserId = new int?();
            }
            else
            {
                modificationAudited.LastUpdatedUserId = userId;
            }
        }
예제 #5
0
        public override Task <int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            foreach (var entry in ChangeTracker.Entries().ToList())
            {
                switch (entry.State)
                {
                case EntityState.Added:
                {
                    IHasCreationTime hasCreation = entry.Entity as IHasCreationTime;
                    if (hasCreation == null)
                    {
                        continue;
                    }
                    hasCreation.CreationTime = DateTime.Now;

                    ICreationAudited creationAudited = entry.Entity as ICreationAudited;
                    if (creationAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        creationAudited.CreatorUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;

                case EntityState.Modified:
                {
                    IHasModificationTime hasModification = entry.Entity as IHasModificationTime;
                    if (hasModification == null)
                    {
                        continue;
                    }
                    hasModification.LastModificationTime = DateTime.Now;

                    IModificationAudited modificationAudited = entry.Entity as IModificationAudited;
                    if (modificationAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        modificationAudited.LastModifierUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;

                case EntityState.Deleted:
                {
                    IHasDeletionTime hasDeletion = entry.Entity as IHasDeletionTime;
                    if (hasDeletion == null)
                    {
                        continue;
                    }
                    entry.Reload();
                    entry.State = EntityState.Modified;

                    hasDeletion.DeletionTime = DateTime.Now;
                    hasDeletion.IsDeleted    = true;

                    IDeletionAudited deletionAudited = entry.Entity as IDeletionAudited;
                    if (deletionAudited == null)
                    {
                        continue;
                    }
                    if (UserIdentifier != null && UserIdentifier.UserId.HasValue)
                    {
                        deletionAudited.DeleterUserId = UserIdentifier.UserId.Value;
                    }
                }
                break;
                }
            }


            return(base.SaveChangesAsync(cancellationToken));
        }