部分更新

Uat_Study
hang 2022-04-18 13:31:40 +08:00
parent 8bb6f1605d
commit 746366a1c2
5 changed files with 68 additions and 31 deletions

View File

@ -35,7 +35,8 @@ namespace IRaCIS.Core.Application.Filter
}
else
{
context.Result = new JsonResult(ResponseOutput.NotOk(" Program exception, please contact the developer! " + (context.Exception.InnerException is null ? (context.Exception.Message + context.Exception.StackTrace) : (context.Exception.InnerException?.Message + context.Exception.InnerException?.StackTrace)) ));
context.Result = new JsonResult(ResponseOutput.NotOk(" Program exception, please contact the developer! " + (context.Exception.InnerException is null ? (context.Exception.Message /*+ context.Exception.StackTrace*/)
: (context.Exception.InnerException?.Message /*+ context.Exception.InnerException?.StackTrace*/)) ));
}

View File

@ -295,9 +295,6 @@ namespace IRaCIS.Application.Services
public async Task<IResponseOutput> ModifyPassword(EditPasswordCommand editPwModel)
{
//验证旧密码OK
var dbUser = await _userRepository.FirstOrDefaultAsync(t => t.Id == _userInfo.Id && t.Password == editPwModel.OldPassWord);

View File

@ -25,8 +25,11 @@ namespace IRaCIS.Application.Services
var c = _dicRepository.Where(t => t.ParentId != null).Select(t => t.MappedValue).First();
CultureInfo culture = CultureInfo.CurrentUICulture;
var dd= _dicRepository.UpdatePartialFields(Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"),
u => new Dictionary() { ParentId = null, Code = "test" }, true).Result;
var d= _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id =Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null,ChildGroup = null,Code = null},true,true).Result;
var d = _dicRepository.UpdateFromDTOAsync(new AddOrEditBasicDic() { Id = Guid.Parse("60d86683-c33b-4349-b672-08da1e91b622"), ParentId = null, ChildGroup = null, Code = null }, true, true).Result;
var a = 123;
var b = _localizer["test{0}", "测试"];

View File

@ -18,10 +18,10 @@ namespace IRaCIS.Core.Infra.EFCore
Task<TEntity> UpdateFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, bool ignoreDtoNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
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<TEntity> UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify);
//TEntity UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify);
}
public interface ICommandRepository<TEntity, TKey> where TEntity : class

View File

@ -20,7 +20,7 @@ using Z.EntityFramework.Plus;
namespace IRaCIS.Core.Infra.EFCore
{
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : Entity
where TEntity : Entity, new()
{
public IMapper _mapper { get; set; }
public IRaCISDBContext _dbContext { get; set; }
@ -136,6 +136,7 @@ namespace IRaCIS.Core.Infra.EFCore
}
}
}
}
public async Task<TEntity> InsertFromDTOAsync<TFrom>(TFrom from, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
@ -146,11 +147,8 @@ namespace IRaCIS.Core.Infra.EFCore
await _dbSet.AddAsync(entity).ConfigureAwait(false);
if (autoSave)
{
await SaveChangesAsync();
await SaveChangesAsync(autoSave);
}
return entity;
}
@ -190,29 +188,67 @@ namespace IRaCIS.Core.Infra.EFCore
}
}
if (autoSave)
{
await SaveChangesAsync();
}
await SaveChangesAsync(autoSave);
return dbBeforEntity;
}
//public TEntity UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
//{
// EntityVerifyAsync(false, verify, id).RunSynchronously();
/// <summary>
/// 通过主键id 和表达式树 更新部分字段 例如 Guid.Parse("8a90c96e-0776-4f7b-82a6-18933d339584"),u => new Dictionary() { ParentId = null, Code = "test" } 默认会去处理更新更新人 更新时间
/// </summary>
/// <param name="id"></param>
/// <param name="updateFactory"></param>
/// <param name="autoSave"></param>
/// <param name="verify"></param>
/// <returns></returns>
public async Task<TEntity> UpdatePartialFields(Guid id, Expression<Func<TEntity, TEntity>> updateFactory, bool autoSave = false, params EntityVerifyExp<TEntity>[] verify)
{
await EntityVerifyAsync(false, verify, id);
// var entity = updateFactory.Compile().Invoke(new TEntity() { Id = id });
var entity = new TEntity() { Id = id };
// var entityEntry = _dbContext.Entry(entity);
// entityEntry.State = EntityState.Detached;
var entityEntry = _dbContext.Entry(entity);
entityEntry.State = EntityState.Detached;
//}
Func<TEntity, TEntity> func = updateFactory.Compile();
List<PropertyInfo> list = ((MemberInitExpression)updateFactory.Body).Bindings.Select<MemberBinding, string>((Func<MemberBinding, string>)(_param1 => _param1.Member.Name)).Select<string, PropertyInfo>((Func<string, PropertyInfo>)(_param1 => (PropertyInfo)typeof(TEntity).GetProperty(_param1, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))).ToList<PropertyInfo>();
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;
}
await SaveChangesAsync(autoSave);
return entityEntry.Entity;
}
private async Task<bool> SaveChangesAsync(bool autoSave)
{
if (autoSave)
{
return await SaveChangesAsync();
}
else
{
return false;
}
}
/// <summary>
/// 部分字段更新 (只更新传递的字段名)
/// 部分字段更新 (只更新传递的字段名 new[] {nameof(User.Name), nameof(User.Age))
/// </summary>
/// <param name="entity"></param>
/// <param name="propertyNames"> 更新的字段数组 </param>
@ -220,10 +256,10 @@ namespace IRaCIS.Core.Infra.EFCore
/// <param name="ignoreEntityNullProperty"></param>
/// <param name="verify"></param>
/// <returns></returns>
public TEntity UpdatePartialFields(TEntity entity, string[] propertyNames,
public async Task<TEntity> UpdatePartialFields(TEntity entity, string[] propertyNames,
bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
{
EntityVerifyAsync(false, verify, entity.Id).RunSynchronously();
await EntityVerifyAsync(false, verify, entity.Id);
var entityEntry = _dbContext.Entry(entity);
entityEntry.State = EntityState.Detached;
@ -241,7 +277,7 @@ namespace IRaCIS.Core.Infra.EFCore
}
/// <summary>
/// 更新 排除某些字段的更新
/// 更新 排除某些字段的更新 排除方式: new[] {nameof(User.Name), nameof(User.Age)
/// </summary>
/// <param name="entity"></param>
/// <param name="propertyNames"></param>
@ -249,15 +285,15 @@ namespace IRaCIS.Core.Infra.EFCore
/// <param name="ignoreEntityNullProperty"></param>
/// <param name="verify"></param>
/// <returns></returns>
public TEntity UpdateExcludeFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
public async Task<TEntity> UpdateExcludeFields(TEntity entity, string[] propertyNames, bool autoSave = false, bool ignoreEntityNullProperty = true, params EntityVerifyExp<TEntity>[] verify)
{
EntityVerifyAsync(false, verify, entity.Id).RunSynchronously();
await EntityVerifyAsync(false, verify, entity.Id);
var entityEntry = _dbContext.Entry(entity);
entityEntry.State = EntityState.Modified;
foreach (var propertyName in propertyNames)
{
_dbContext.Entry(entity).Property(propertyName).IsModified = false;