EF更新少量字段需要解决两个问题
1.动态的将需要更新的字段提取出来
2.将提取出来的字段设为更新状态
通常更新的时候,都是根据条件将实体取出来,然后赋值字段,最后更新整个实体,所以在方法上看似是更新少量字段,其实是更新了所有字段,比较浪费性能
获取要更新的字段列表
/// <summary> /// 获取要更新的字段 /// </summary> /// <param name="updateAction"></param> /// <returns></returns> private List<string> GetUpdateColumns(Action<TEntity> updateAction) { List<string> modifyColumns = new List<string>(); TEntity modifyModel = new TEntity(); updateAction(modifyModel); PropertyInfo[] proArr = typeof(TEntity).GetProperties(); foreach (PropertyInfo info in proArr) { if (info.GetValue(modifyModel) != null) { modifyColumns.Add(info.Name); } } return modifyColumns; }
更新
/// <summary> /// 更新对象部分属性 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="predicate"></param> /// <param name="updateAction"></param> /// <returns></returns> public TResult Update(Expression<Func<TEntity, bool>> predicate, Action<TEntity> updateAction) { List<string> modifyColumns = GetUpdateColumns(updateAction); //dbContext.Configuration.AutoDetectChangesEnabled = true; var _model = dbContext.Set<TEntity>().AsNoTracking().Where(predicate).ToList(); if (_model == null) return new TResult(false, "参数为NULL"); _model.ForEach(p => { updateAction(p); dbContext.Set<TEntity>().Attach(p); var stateEntry = ((IObjectContextAdapter)dbContext).ObjectContext.ObjectStateManager.GetObjectStateEntry(p); modifyColumns.ForEach(m => stateEntry.SetModifiedProperty(m)); //dbContext.Entry<TEntity>(p).State = EntityState.Modified; }); return Save(EntityState.Modified); }