EF更新指定的字段

EF更新指定字段,代码如下:

接口类:

    /// <summary>
        /// 更新指定字段
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="fileds">更新字段数组</param>
        void UpdateEntityFields(T entity, List<string> fileds);

实现接口类:

        public void UpdateEntityFields(T entity, List<string> fileds)
        {
            if (entity != null&&fileds!=null)
            {
                dbContext.CreateObjectSet<T>().Attach(entity);
                var SetEntry = ((IObjectContextAdapter)dbContext).ObjectContext.
                    ObjectStateManager.GetObjectStateEntry(entity);
                foreach (var t in fileds)
                {
                    SetEntry.SetModifiedProperty(t);
                }
            }
        }

需要引用命名空间:

using System.Data.Entity.Infrastructure;

从村长dudu哪里学到的,我也记录下,求路过的大神,帮忙改成Lambda形式的强类型 谢谢

如果大家还迷茫,这里有示例代码

Entity Framework 同一个上下文中,如何进行对同一个实体进行指定字段更新

原文地址:https://www.cnblogs.com/flyfish2012/p/2857616.html