修改仓储
parent
2b84cdd5a6
commit
cf08b85811
|
@ -224,39 +224,46 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息</summary>
|
|
||||||
public async Task<bool> UpdatePartialAsync(TEntity entity, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default)
|
|
||||||
|
|
||||||
|
/// <summary>EF跟踪方式 外层先有查询好的完成实体,再更新部分字段 稽查的时候需要完整的实体信息</summary>
|
||||||
|
public async Task<bool> UpdatePartialAsync(TEntity waitModifyEntity, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var entityEntry = _dbContext.Entry(entity);
|
var entityEntry = _dbContext.Entry(waitModifyEntity);
|
||||||
|
|
||||||
|
|
||||||
entityEntry.State = EntityState.Detached;
|
entityEntry.State = EntityState.Detached;
|
||||||
|
|
||||||
|
|
||||||
List<PropertyInfo> list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name)
|
ModifyPartialFiled(waitModifyEntity, updateFactory);
|
||||||
.Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList();
|
|
||||||
|
|
||||||
Func<TEntity, TEntity> func = updateFactory.Compile();
|
|
||||||
|
|
||||||
TEntity applyObj = func(entity);
|
|
||||||
|
|
||||||
foreach (PropertyInfo prop in list)
|
|
||||||
{
|
|
||||||
object value = prop.GetValue((object)applyObj);
|
|
||||||
prop.SetValue((object)entity, value);
|
|
||||||
|
|
||||||
_dbContext.Entry(entity).Property(prop.Name).IsModified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await SaveChangesAsync(autoSave);
|
return await SaveChangesAsync(autoSave);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdatePartialSearchFirstAsync(Expression<Func<TEntity, bool>> updateFilter,
|
||||||
|
Expression<Func<TEntity, TEntity>> updateFactory,
|
||||||
|
bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (updateFilter == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("更新过滤条件不允许为空", nameof(updateFilter));
|
||||||
|
}
|
||||||
|
|
||||||
|
var searchEntityList = await _dbSet.AsNoTracking().Where(updateFilter).ToListAsync();
|
||||||
|
|
||||||
|
foreach (var needUpdateEntity in searchEntityList)
|
||||||
|
{
|
||||||
|
await UpdatePartialAsync(needUpdateEntity, updateFactory, autoSave);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息</summary>
|
/// <summary>EF跟踪方式 先查询出来,再更新部分字段 稽查的时候需要完整的实体信息</summary>
|
||||||
public async Task<TEntity> UpdatePartialSearchFirstAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
|
public async Task<TEntity> UpdatePartialSearchFirstAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
|
||||||
bool autoSave = false, CancellationToken cancellationToken = default)
|
bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
//不跟踪 查询出来的实体就是Detached
|
||||||
var searchEntity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id);
|
var searchEntity = await _dbSet.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id);
|
||||||
|
|
||||||
if (searchEntity == null)
|
if (searchEntity == null)
|
||||||
|
@ -266,21 +273,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<PropertyInfo> list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name)
|
ModifyPartialFiled(searchEntity, updateFactory);
|
||||||
.Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList();
|
|
||||||
|
|
||||||
Func<TEntity, TEntity> func = updateFactory.Compile();
|
|
||||||
|
|
||||||
TEntity applyObj = func(searchEntity);
|
|
||||||
|
|
||||||
foreach (PropertyInfo prop in list)
|
|
||||||
{
|
|
||||||
object value = prop.GetValue((object)applyObj);
|
|
||||||
prop.SetValue((object)searchEntity, value);
|
|
||||||
|
|
||||||
_dbContext.Entry(searchEntity).Property(prop.Name).IsModified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await SaveChangesAsync(autoSave);
|
await SaveChangesAsync(autoSave);
|
||||||
|
@ -289,6 +282,24 @@ namespace IRaCIS.Core.Infra.EFCore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void ModifyPartialFiled(TEntity waitModifyEntity, Expression<Func<TEntity, TEntity>> updateFactory)
|
||||||
|
{
|
||||||
|
List<PropertyInfo> list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name)
|
||||||
|
.Select(propName => typeof(TEntity).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)).ToList();
|
||||||
|
|
||||||
|
Func<TEntity, TEntity> func = updateFactory.Compile();
|
||||||
|
|
||||||
|
TEntity applyObj = func(waitModifyEntity);
|
||||||
|
|
||||||
|
foreach (PropertyInfo prop in list)
|
||||||
|
{
|
||||||
|
object value = prop.GetValue(applyObj);
|
||||||
|
prop.SetValue(waitModifyEntity, value);
|
||||||
|
|
||||||
|
_dbContext.Entry(waitModifyEntity).Property(prop.Name).IsModified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>EF跟踪方式 删除</summary>
|
/// <summary>EF跟踪方式 删除</summary>
|
||||||
public async Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
public async Task<bool> DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
|
|
Loading…
Reference in New Issue