部分更新
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user