整理仓储
continuous-integration/drone/push Build is running
Details
continuous-integration/drone/push Build is running
Details
parent
581e412106
commit
961ca49f76
|
@ -22,19 +22,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
Task<TEntity> UpdateFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 空和Empty不更新
|
||||
/// </summary>
|
||||
/// <typeparam name="TFrom"></typeparam>
|
||||
/// <param name="from"></param>
|
||||
/// <param name="autoSave"></param>
|
||||
/// <param name="ignoreDtoNullProperty"></param>
|
||||
/// <param name="verify"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> NotUpdateEmptyAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
|
||||
|
||||
#region EF 跟踪方式删除 (先查询完整实体,再删除)
|
||||
|
||||
/// <summary>批量删除,EF跟踪方式(所有查询出来,再删除 浪费性能,但是稽查 或者触发某些操作时,需要知道数据库实体信息 不可避免用这种)</summary>
|
||||
|
@ -65,15 +52,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
#endregion
|
||||
|
||||
#region EF跟踪方式 部分字段更新,不会去数据库查询完整实体
|
||||
|
||||
/// <summary>EF跟踪方式 生成 部分字段立即更新,不会去数据库查询完整的实体</summary>
|
||||
|
||||
Task<bool> UpdatePartialNowNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段更新,不会去数据库查询完整的实体</summary>
|
||||
Task UpdatePartialNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
|
||||
#endregion
|
||||
|
||||
#region 不走EF 跟踪机制,直接生成sql 批量更新或者删除
|
||||
|
||||
|
@ -88,10 +67,18 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
|
||||
#region 不常用
|
||||
//Task<TEntity> UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
/// <summary>EF跟踪方式 生成 部分字段立即更新,不会去数据库查询完整的实体,不符合我们稽查的需求</summary>
|
||||
|
||||
Task<bool> UpdatePartialNowNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段更新,不会去数据库查询完整的实体,不符合我们稽查的需求</summary>
|
||||
//Task UpdatePartialNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary> 不常用 暂时废弃</summary>
|
||||
Task<TEntity> UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
|
||||
|
||||
|
||||
}
|
||||
|
@ -111,13 +98,18 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> exp = null, bool isTracking = false,bool ignoreQueryFilters = false);
|
||||
|
||||
/// <summary>
|
||||
///跟踪 查询单个实体,会出现NUll
|
||||
///跟踪 查询单个实体,会出现NUll 可以直接在实体对属性进行修改,然后save
|
||||
/// </summary>
|
||||
/// <param name="exp"></param>
|
||||
/// <param name="ignoreQueryFilters"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
||||
|
||||
/// <summary>
|
||||
/// 不跟踪,查询单个实体 会出现NUll 配合 UpdateAsync 可以生成部分字段更新到数据库
|
||||
/// </summary>
|
||||
/// <param name="exp"></param>
|
||||
/// <param name="ignoreQueryFilters"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> FirstOrDefaultNoTrackingAsync(Expression<Func<TEntity, bool>> exp = null, bool ignoreQueryFilters = false);
|
||||
|
||||
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> exp, bool ignoreQueryFilters = false);
|
||||
|
|
|
@ -90,8 +90,7 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
|
||||
#region 异步 EF 跟踪 部分字段更新
|
||||
|
||||
#region 异步 EF 跟踪 自动生成 更新 和删除语句
|
||||
|
||||
/// <summary>用前端传递的视图模型字段,更新,同时返回数据库该条记录的原始信息,方便对比某些字段是否更改,进行相应的逻辑操作</summary>
|
||||
|
||||
|
@ -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<TEntity> NotUpdateEmptyAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
|
||||
var entity = _mapper.Map<TEntity>(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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段更新, 跟踪的实体仅有修改的属性的值有具体意义,没有从数据库查询完整的实体</summary>
|
||||
|
||||
public async Task UpdatePartialNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
|
||||
await _dbContext.EntityVerifyAsync(false, verify, id);
|
||||
|
||||
var entity = new TEntity() { Id = id };
|
||||
|
||||
_dbContext.EntityModifyPartialFiled(entity, updateFactory);
|
||||
|
||||
|
||||
await SaveChangesAsync(autoSave);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段立即更新,默认会去处理更新更新人 更新时间 </summary>
|
||||
public async Task<bool> UpdatePartialNowNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
|
||||
params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
await _dbContext.EntityVerifyAsync(false, verify, id);
|
||||
|
||||
var entity = new TEntity() { Id = id };
|
||||
|
||||
_dbContext.EntityModifyPartialFiled(entity, updateFactory);
|
||||
|
||||
return await SaveChangesAsync(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region 异步 EF 跟踪 自动生成 更新 和删除语句
|
||||
|
||||
|
||||
/// <summary>EF跟踪方式 更新,全字段更新 不好</summary>
|
||||
public async Task<bool> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
|
||||
return await SaveChangesAsync(autoSave);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>EF跟踪方式 外层先有查询好的完成实体,再更新部分字段 稽查的时候需要完整的实体信息</summary>
|
||||
|
@ -349,7 +259,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 不走EF 跟踪机制的删除 更新 以及批量操作
|
||||
|
||||
/// <summary>批量删除,相当于原生sql, 没用EF跟踪方式(所有查询出来,再删除 浪费性能)</summary>
|
||||
|
@ -370,6 +279,8 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
|
||||
}
|
||||
|
||||
/// <summary>EF core 7+ 原生批量更新方法 </summary>
|
||||
|
||||
public async Task<bool> ExecuteUpdateAsync(Expression<Func<TEntity, bool>> where, Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls)
|
||||
{
|
||||
return await _dbContext.ExecuteUpdateAsync(where, setPropertyCalls);
|
||||
|
@ -378,8 +289,6 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
#region 保存 、忽略 、验证
|
||||
public async Task<TEntity> InsertOrUpdateAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
|
@ -458,6 +367,47 @@ namespace IRaCIS.Core.Infra.EFCore
|
|||
#endregion
|
||||
|
||||
#region 不常用
|
||||
|
||||
|
||||
/// <summary>EF跟踪方式 更新,全字段更新 不好</summary>
|
||||
public async Task<bool> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_dbSet.Update(entity);
|
||||
|
||||
return await SaveChangesAsync(autoSave);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段立即更新,默认会去处理更新更新人 更新时间 </summary>
|
||||
public async Task<bool> UpdatePartialNowNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
|
||||
params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
await _dbContext.EntityVerifyAsync(false, verify, id);
|
||||
|
||||
var entity = new TEntity() { Id = id };
|
||||
|
||||
_dbContext.EntityModifyPartialFiled(entity, updateFactory);
|
||||
|
||||
return await SaveChangesAsync(true);
|
||||
}
|
||||
|
||||
/// <summary> EF跟踪方式 生成 部分字段更新, 跟踪的实体仅有修改的属性的值有具体意义,没有从数据库查询完整的实体</summary>
|
||||
|
||||
public async Task UpdatePartialNoQueryAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
|
||||
{
|
||||
|
||||
await _dbContext.EntityVerifyAsync(false, verify, id);
|
||||
|
||||
var entity = new TEntity() { Id = id };
|
||||
|
||||
_dbContext.EntityModifyPartialFiled(entity, updateFactory);
|
||||
|
||||
|
||||
await SaveChangesAsync(autoSave);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>EF跟踪方式 生成 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age))</summary>
|
||||
public async Task<TEntity> UpdatePartialFieldsAsync(TEntity entity, string[] propertyNames,
|
||||
bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
|
||||
|
|
Loading…
Reference in New Issue