首页 > 代码库 > EF 指定字段修改

EF 指定字段修改

技术分享
 public virtual void Modify(T model, params string[] ProNames)        {             DbEntityEntry entry = db.Entry<T>(model);            entry.OriginalValues.SetValues(model);  // 没有此句修改不了            entry.State = System.Data.EntityState.Unchanged;             foreach (string Name in ProNames)            {                entry.Property(Name).IsModified = true;            }            //return db.SaveChanges();         }                                                  public virtual void Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] ModifiedProNames)        {            List<T> listModifeding = db.Set<T>().Where(whereLambda).ToList();            listModifeding.ForEach(s =>            {                Modify(model, ModifiedProNames);            });            #region 重构前            //Type t = typeof(T);            //List<PropertyInfo> proInfo = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();            //Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>();            //proInfo.ForEach(p =>            //    {            //        if (ModifiedProNames.Contains(p.Name))            //        {            //            dictPros.Add(p.Name, p);            //        }            //    });            //foreach (string Name in ModifiedProNames)            //{            //    if (dictPros.ContainsKey(Name))            //    {            //        PropertyInfo PInfo = dictPros[Name];            //        object NewValue = http://www.mamicode.com/PInfo.GetValue(model, null);>//        foreach (T us in listModifeding)            //        {            //            PInfo.SetValue(us, NewValue, null);            //        }            //    }            //}             #endregion            ////return db.SaveChanges();        }
EF Modify

 

EF 指定字段修改