• EF 如何更新少量字段


    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);
            }
  • 相关阅读:
    platform_device和platform_driver
    理解和认识udev
    platform_device和platform_driver
    bzImage的概要生成过程
    shell 字符表
    分析mtk6516如何加入自己的驱动
    理解和使用Linux的硬件抽象层HAL
    bzImage的概要生成过程
    理解和认识udev
    shell 字符表
  • 原文地址:https://www.cnblogs.com/njcxwz/p/6208331.html
Copyright © 2020-2023  润新知