部分更新 立即提交事务

Test.EIImageViewer
hang 2022-04-19 16:40:30 +08:00
parent 0f6bae867c
commit 01c38bbd63
2 changed files with 25 additions and 10 deletions

View File

@ -20,9 +20,13 @@ namespace IRaCIS.Core.Infra.EFCore
Task<TEntity> UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify); Task<TEntity> UpdatePartialFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
Task<bool> UpdatePartialFieldsNow(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, params EntityVerifyExp<TEntity>[] verify);
Task UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify); Task UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
Task<bool> BatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter); Task<bool> BatchDeleteAsync(Expression<Func<TEntity, bool>> deleteFilter);
Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> updateFactory); Task<bool> BatchUpdateAsync(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TEntity>> updateFactory);

View File

@ -31,7 +31,7 @@ namespace IRaCIS.Core.Infra.EFCore
public IUserInfo _userInfo { get; set; } public IUserInfo _userInfo { get; set; }
public Repository(IRaCISDBContext dbContext, IMapper mapper,IUserInfo userInfo) public Repository(IRaCISDBContext dbContext, IMapper mapper, IUserInfo userInfo)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_mapper = mapper; _mapper = mapper;
@ -343,7 +343,7 @@ namespace IRaCIS.Core.Infra.EFCore
var dbEntityProp = typeof(TEntity).GetProperties(); var dbEntityProp = typeof(TEntity).GetProperties();
foreach (var propertyInfo in from.GetType().GetProperties()) foreach (var propertyInfo in from.GetType().GetProperties())
{ {
if (propertyInfo.GetValue(from) == null && dbEntityProp.Any(t=>t.Name== propertyInfo.Name)) if (propertyInfo.GetValue(from) == null && dbEntityProp.Any(t => t.Name == propertyInfo.Name))
{ {
_dbContext.Entry(dbEntity).Property(propertyInfo.Name).IsModified = false; _dbContext.Entry(dbEntity).Property(propertyInfo.Name).IsModified = false;
} }
@ -378,6 +378,21 @@ namespace IRaCIS.Core.Infra.EFCore
/// <param name="verify"></param> /// <param name="verify"></param>
/// <returns></returns> /// <returns></returns>
public async Task UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify) public async Task UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
{
await SetPartialFieldUpdateAsync(id, updateFactory, verify);
await SaveChangesAsync(autoSave);
}
public async Task<bool> UpdatePartialFieldsNow(Guid id, Expression<Func<TEntity, TEntity>> updateFactory,
params EntityVerifyExp<TEntity>[] verify)
{
await SetPartialFieldUpdateAsync(id, updateFactory, verify);
return await SaveChangesAsync(true);
}
private async Task SetPartialFieldUpdateAsync(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, params EntityVerifyExp<TEntity>[] verify)
{ {
await EntityVerifyAsync(false, verify, id); await EntityVerifyAsync(false, verify, id);
@ -403,9 +418,6 @@ namespace IRaCIS.Core.Infra.EFCore
_dbContext.Entry(entity).Property(prop.Name).IsModified = true; _dbContext.Entry(entity).Property(prop.Name).IsModified = true;
} }
await SaveChangesAsync(autoSave);
#region Test #region Test
@ -422,7 +434,6 @@ namespace IRaCIS.Core.Infra.EFCore
} }
/// <summary> /// <summary>
/// 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age)) /// 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age))
/// new Dictionary() { ParentId = null, Code = "test"Id=Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584")},new[] {nameof(Dictionary.Name), nameof(Dictionary.Age)) /// new Dictionary() { ParentId = null, Code = "test"Id=Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584")},new[] {nameof(Dictionary.Name), nameof(Dictionary.Age))