修改仓储
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user