/// <summary> /// 增加单个实体,实体映射由用户自定义,所有增加单个实体的最终实现 /// 请在外部提交更改 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities"></param> /// <param name="dto"></param> /// <param name="addFunc"></param> /// <param name="userId"></param> public static TEntity AddInternal <TEntity, TDto, TKey>(this DbSet <TEntity> entities, TDto dto, Func <TDto, TEntity> addFunc, TKey userId, TKey tenantId = default) where TEntity : class, IIdField <TKey> { var entity = addFunc(dto); if (string.IsNullOrEmpty(entity.Id?.ToString())) { entity.Id = IdGenerator.Generate <TKey>(); } UpdateEntityCommonField(entity, EEntityOperType.Add, userId, tenantId); TenantHelper.CheckEntityTenantOper(EEntityOperType.Delete, entity, userId, tenantId); entities.Add(entity); return(entity); }
/// <summary> /// 删除实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities">dbset</param> /// <param name="id">id</param> /// <param name="userId"></param> /// <param name="tenantId"></param> /// <param name="existEntities">从existEntities里删除而不是从dbset里删除</param> public static void RemoveById <TEntity, TKey>(this DbSet <TEntity> entities, TKey id, TKey userId, TKey tenantId = default) where TEntity : class, IIdField <TKey> { var entity = entities.Find(id); if (entity != null) { TenantHelper.CheckEntityTenantOper(EEntityOperType.Delete, entity, userId, tenantId); if (entity is ISoftDelete entitySoftDeleteEntity) { UpdateEntityCommonField(entity, EEntityOperType.Delete, userId, default); } else { entities.Remove(entity); } } }
/// <summary> /// 增加或更新的最终实现 /// 请在外部提交更改 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities"></param> /// <param name="dto">要增加或更新的对象</param> /// <param name="addFunc"></param> /// <param name="updateFunc"></param> /// <param name="userId"></param> /// <param name="existEntities"></param> public static TEntity AddOrUpdateInternal <TEntity, TDto, TKey>(this DbSet <TEntity> entities, TDto dto, Func <TDto, TEntity> addFunc, Action <TDto, TEntity> updateFunc, TKey userId, TKey tenantId = default, List <TEntity> existEntities = null) where TEntity : class, IIdField <TKey> where TDto : class, IIdField <TKey> { var entity = existEntities == null?entities.Find(dto.Id) : existEntities.FirstOrDefault(a => a.Id.Equals(dto.Id)); if (entity == null) { entity = AddInternal(entities, dto, addFunc, userId, tenantId); } else { //update TenantHelper.CheckEntityTenantOper(EEntityOperType.Update, entity, userId, tenantId); updateFunc(dto, entity); UpdateEntityCommonField(entity, EEntityOperType.Update, userId, tenantId); } return(entity); }
public virtual void RemoveUser(string userKey) { var userEntity = _db.Set <TUser>().Find(userKey); TenantHelper.CheckEntityTenantOper(EEntityOperType.Delete, userEntity, CurrentUserId, CurrentTenantId); if (userEntity is ISoftDelete entitySoftDeleteEntity) { entitySoftDeleteEntity.IsDeleted = true; } else { _db.Set <TUser>().Remove(userEntity); } //roleUser _db.Set <TUserRole>().AsEnumerable().Where(a => a.GetUserKey() == userKey).ToList().ForEach(userRole => { _db.Remove(userRole); }); _db.SaveChanges(); _memoryCache.Remove(userCacheKey); }
public virtual void RemoveRole(string roleKey) { // 删除角色为真删除,不保留历史的角色和角色资源关系 var roleEntity = GetAllRole().FirstOrDefault(a => a.GetKey() == roleKey) as TRole; TenantHelper.CheckEntityTenantOper(EEntityOperType.Delete, roleEntity, CurrentUserId, CurrentTenantId); if (roleEntity != null) { _db.Set <TRole>().Remove(roleEntity); } //roleUser _db.Set <TUserRole>().AsEnumerable().Where(a => a.GetRoleKey() == roleKey).ToList().ForEach(userRole => { _db.Remove(userRole); }); //roleResource _db.Set <TRoleResource>().AsEnumerable().Where(a => a.GetRoleKey() == roleKey).ToList().ForEach(roleResource => { _db.Remove(roleResource); }); _db.SaveChanges(); _memoryCache.Remove(roleCacheKey); }