修改仓储

Uat_Study
hang 2022-04-26 16:25:02 +08:00
parent 0ca89786fd
commit f118dd6e42
4 changed files with 37 additions and 13 deletions

View File

@ -191,7 +191,7 @@ namespace IRaCIS.Application.Services
}
var isSuccess = await _subjectRepository.BatchDeleteAsync(u => u.Id == id);
await _subjectVisitRepository.TrackingBatchDeleteAsync(u => u.SubjectId == id);
await _subjectVisitRepository.TrackingDeleteFromQueryAsync(u => u.SubjectId == id);
var subvisit = await _subjectVisitRepository.Where(x => x.SubjectId == id).ToListAsync();

View File

@ -23,7 +23,7 @@ namespace IRaCIS.Application.Services
public string Get(testModel testModel)
{
var tt= _dicRepository.QueryThenPartiallyUpdateAsync(Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff"), t => new Dictionary() { Description = "xxxxx" }, true).Result;
var tt= _dicRepository.TrackingUpdateFromQueryAsync(Guid.Parse("e2b97a6c-35a6-4aa3-7f27-08da13ab33ff"), t => new Dictionary() { Description = "xxxxx" }, true).Result;
//var d = _repository.Where<User>(t => t.FullName.Contains("cc")).Select(t => t.FullName).FirstOrDefault();

View File

@ -34,18 +34,22 @@ namespace IRaCIS.Core.Infra.EFCore
Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> updateFactory);
/// <summary>批量删除EF跟踪方式所有查询出来再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
Task<List<TEntity>> TrackingBatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter);
Task<List<TEntity>> TrackingDeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false);
Task<TEntity> TrackingDeleteFromQueryAsync(Guid id, bool autoSave = false);
/// <summary> EF跟踪方式 已有查询好的,再更新部分字段 稽查的时候需要完整的实体信息</summary>
Task<bool> PartialUpdateAsync(TEntity entity, Expression<Func<TEntity, TEntity>> updateFactory,
Task<bool> TrackingUpdateAsync(TEntity entity, Expression<Func<TEntity, TEntity>> updateFactory,
bool autoSave = false, CancellationToken cancellationToken = default);
/// <summary> EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息</summary>
Task<TEntity> QueryThenPartiallyUpdateAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
Task<TEntity> TrackingUpdateFromQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
bool autoSave = false, CancellationToken cancellationToken = default);
Task QueryThenPartiallyUpdateAsync(Expression<Func<TEntity, bool>> updateFilter,
Task TrackingUpdateFromQueryAsync(Expression<Func<TEntity, bool>> updateFilter,
Expression<Func<TEntity, TEntity>> updateFactory,
bool autoSave = false, CancellationToken cancellationToken = default);

View File

@ -224,7 +224,9 @@ namespace IRaCIS.Core.Infra.EFCore
}
public async Task QueryThenPartiallyUpdateAsync(Expression<Func<TEntity, bool>> updateFilter,
public async Task TrackingUpdateFromQueryAsync(Expression<Func<TEntity, bool>> updateFilter,
Expression<Func<TEntity, TEntity>> updateFactory,
bool autoSave = false, CancellationToken cancellationToken = default)
{
@ -237,14 +239,14 @@ namespace IRaCIS.Core.Infra.EFCore
foreach (var needUpdateEntity in searchEntityList)
{
await PartialUpdateAsync(needUpdateEntity, updateFactory, autoSave);
await TrackingUpdateAsync(needUpdateEntity, updateFactory, autoSave);
}
}
/// <summary>EF跟踪方式 外层先有查询好的完成实体,再更新部分字段 稽查的时候需要完整的实体信息</summary>
public async Task<bool> PartialUpdateAsync(TEntity waitModifyEntity, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default)
public async Task<bool> TrackingUpdateAsync(TEntity waitModifyEntity, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default)
{
var entityEntry = _dbContext.Entry(waitModifyEntity);
entityEntry.State = EntityState.Detached;
@ -260,11 +262,11 @@ namespace IRaCIS.Core.Infra.EFCore
/// <summary>EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息</summary>
public async Task<TEntity> QueryThenPartiallyUpdateAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
public async Task<TEntity> TrackingUpdateFromQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
bool autoSave = false, CancellationToken cancellationToken = default)
{
//不跟踪 查询出来的实体就是Detached
var searchEntity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id);
var searchEntity = await _dbSet.AsNoTracking().IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == id);
if (searchEntity == null)
{
@ -311,14 +313,29 @@ namespace IRaCIS.Core.Infra.EFCore
{
_dbSet.Remove(entity);
return await SaveChangesAsync(autoSave);
}
/// <summary>EF跟踪方式查询出来再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
public async Task<TEntity> TrackingDeleteFromQueryAsync(Guid id, bool autoSave = false)
{
var waitDelete = await _dbSet.IgnoreQueryFilters().Where(t=>t.Id== id).FirstOrDefaultAsync();
if (waitDelete == null)
{
throw new BusinessValidationFailedException(
" Delete object not exist in db,Please check if the parameter Id is passed incorrectly");
}
await DeleteAsync(waitDelete, autoSave);
return waitDelete;
}
/// <summary>批量删除EF跟踪方式所有查询出来再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
public async Task<List<TEntity>> TrackingBatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter)
public async Task<List<TEntity>> TrackingDeleteFromQueryAsync(Expression<Func<TEntity, bool>> deleteFilter, bool autoSave = false)
{
var waitDeleteList = await _dbSet.IgnoreQueryFilters().Where(deleteFilter).ToListAsync();
@ -326,6 +343,9 @@ namespace IRaCIS.Core.Infra.EFCore
{
await DeleteAsync(deleteItem, false);
}
await SaveChangesAsync(autoSave);
return waitDeleteList;
}