diff --git a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs index aa3bfc347..68e05af17 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/ICommandRepository.cs @@ -22,19 +22,6 @@ namespace IRaCIS.Core.Infra.EFCore Task UpdateFromDTOAsync(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp[] verify); - /// - /// 空和Empty不更新 - /// - /// - /// - /// - /// - /// - /// - Task NotUpdateEmptyAsync(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp[] verify); - - - #region EF 跟踪方式删除 (先查询完整实体,再删除) /// 批量删除,EF跟踪方式(所有查询出来,再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种) @@ -65,15 +52,7 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - #region EF跟踪方式 部分字段更新,不会去数据库查询完整实体 - /// EF跟踪方式 生成 部分字段立即更新,不会去数据库查询完整的实体 - - Task UpdatePartialNowNoQueryAsync(Guid id, Expression> updateFactory, params EntityVerifyExp[] verify); - - /// EF跟踪方式 生成 部分字段更新,不会去数据库查询完整的实体 - Task UpdatePartialNoQueryAsync(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify); - #endregion #region 不走EF 跟踪机制,直接生成sql 批量更新或者删除 @@ -88,10 +67,18 @@ namespace IRaCIS.Core.Infra.EFCore #endregion + #region 不常用 + //Task UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify); + + /// EF跟踪方式 生成 部分字段立即更新,不会去数据库查询完整的实体,不符合我们稽查的需求 + + Task UpdatePartialNowNoQueryAsync(Guid id, Expression> updateFactory, params EntityVerifyExp[] verify); + + /// EF跟踪方式 生成 部分字段更新,不会去数据库查询完整的实体,不符合我们稽查的需求 + //Task UpdatePartialNoQueryAsync(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify); + #endregion - /// 不常用 暂时废弃 - Task UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify); } @@ -111,13 +98,18 @@ namespace IRaCIS.Core.Infra.EFCore Task FirstAsync(Expression> exp = null, bool isTracking = false,bool ignoreQueryFilters = false); /// - ///跟踪 查询单个实体,会出现NUll + ///跟踪 查询单个实体,会出现NUll 可以直接在实体对属性进行修改,然后save /// /// /// /// Task FirstOrDefaultAsync(Expression> exp = null, bool ignoreQueryFilters = false); - + /// + /// 不跟踪,查询单个实体 会出现NUll 配合 UpdateAsync 可以生成部分字段更新到数据库 + /// + /// + /// + /// Task FirstOrDefaultNoTrackingAsync(Expression> exp = null, bool ignoreQueryFilters = false); Task AnyAsync(Expression> exp, bool ignoreQueryFilters = false); diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index af822f27b..9b7b7c487 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -90,8 +90,7 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - #region 异步 EF 跟踪 部分字段更新 - + #region 异步 EF 跟踪 自动生成 更新 和删除语句 /// 用前端传递的视图模型字段,更新,同时返回数据库该条记录的原始信息,方便对比某些字段是否更改,进行相应的逻辑操作 @@ -108,7 +107,7 @@ namespace IRaCIS.Core.Infra.EFCore if (dbEntity == null) { - + throw new BusinessValidationFailedException(_localizer["Repository_UpdateError"]); } @@ -139,95 +138,6 @@ namespace IRaCIS.Core.Infra.EFCore } - public async Task NotUpdateEmptyAsync(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp[] verify) - { - - var entity = _mapper.Map(from); - - //await EntityVerifyAsync(false, verify, entity.Id); - - await _dbContext.EntityVerifyAsync(false, verify, entity.Id); - - var dbEntity = await _dbSet.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == entity.Id).ConfigureAwait(false); - - if (dbEntity == null) - { - - throw new BusinessValidationFailedException(_localizer["Repository_UpdateError"]); - } - - var dbBeforEntity = dbEntity.Clone(); - - - _mapper.Map(from, dbEntity); - - - //为null 或者 string.empty 不更新 - if (ignoreDtoNullProperty) - { - var dbEntityProp = typeof(TEntity).GetProperties(); - foreach (var propertyInfo in from.GetType().GetProperties()) - { - if ((propertyInfo.GetValue(from) == null|| propertyInfo.GetValue(from).ToString()==string.Empty) && dbEntityProp.Any(t => t.Name == propertyInfo.Name)) - { - _dbContext.Entry(dbEntity).Property(propertyInfo.Name).IsModified = false; - } - } - } - - await SaveChangesAsync(autoSave); - - - return dbBeforEntity; - } - - - /// EF跟踪方式 生成 部分字段更新, 跟踪的实体仅有修改的属性的值有具体意义,没有从数据库查询完整的实体 - - public async Task UpdatePartialNoQueryAsync(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify) - { - - await _dbContext.EntityVerifyAsync(false, verify, id); - - var entity = new TEntity() { Id = id }; - - _dbContext.EntityModifyPartialFiled(entity, updateFactory); - - - await SaveChangesAsync(autoSave); - - } - - - /// EF跟踪方式 生成 部分字段立即更新,默认会去处理更新更新人 更新时间 - public async Task UpdatePartialNowNoQueryAsync(Guid id, Expression> updateFactory, - params EntityVerifyExp[] verify) - { - await _dbContext.EntityVerifyAsync(false, verify, id); - - var entity = new TEntity() { Id = id }; - - _dbContext.EntityModifyPartialFiled(entity, updateFactory); - - return await SaveChangesAsync(true); - } - - - - - #endregion - - - #region 异步 EF 跟踪 自动生成 更新 和删除语句 - - - /// EF跟踪方式 更新,全字段更新 不好 - public async Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) - { - _dbSet.Update(entity); - - return await SaveChangesAsync(autoSave); - } /// EF跟踪方式 外层先有查询好的完成实体,再更新部分字段 稽查的时候需要完整的实体信息 @@ -349,7 +259,6 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - #region 不走EF 跟踪机制的删除 更新 以及批量操作 /// 批量删除,相当于原生sql, 没用EF跟踪方式(所有查询出来,再删除 浪费性能) @@ -370,6 +279,8 @@ namespace IRaCIS.Core.Infra.EFCore } + /// EF core 7+ 原生批量更新方法 + public async Task ExecuteUpdateAsync(Expression> where, Expression, SetPropertyCalls>> setPropertyCalls) { return await _dbContext.ExecuteUpdateAsync(where, setPropertyCalls); @@ -378,8 +289,6 @@ namespace IRaCIS.Core.Infra.EFCore #endregion - - #region 保存 、忽略 、验证 public async Task InsertOrUpdateAsync(TFrom from, bool autoSave = false, params EntityVerifyExp[] verify) { @@ -458,6 +367,47 @@ namespace IRaCIS.Core.Infra.EFCore #endregion #region 不常用 + + + /// EF跟踪方式 更新,全字段更新 不好 + public async Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + { + _dbSet.Update(entity); + + return await SaveChangesAsync(autoSave); + } + + + + /// EF跟踪方式 生成 部分字段立即更新,默认会去处理更新更新人 更新时间 + public async Task UpdatePartialNowNoQueryAsync(Guid id, Expression> updateFactory, + params EntityVerifyExp[] verify) + { + await _dbContext.EntityVerifyAsync(false, verify, id); + + var entity = new TEntity() { Id = id }; + + _dbContext.EntityModifyPartialFiled(entity, updateFactory); + + return await SaveChangesAsync(true); + } + + /// EF跟踪方式 生成 部分字段更新, 跟踪的实体仅有修改的属性的值有具体意义,没有从数据库查询完整的实体 + + public async Task UpdatePartialNoQueryAsync(Guid id, Expression> updateFactory, bool autoSave = false, params EntityVerifyExp[] verify) + { + + await _dbContext.EntityVerifyAsync(false, verify, id); + + var entity = new TEntity() { Id = id }; + + _dbContext.EntityModifyPartialFiled(entity, updateFactory); + + + await SaveChangesAsync(autoSave); + + } + /// EF跟踪方式 生成 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age)) public async Task UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp[] verify)