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 = PInfo.GetValue(model, null); // foreach (T us in listModifeding) // { // PInfo.SetValue(us, NewValue, null); // } // } //} #endregion ////return db.SaveChanges(); }
EF Modify
时间: 2024-10-10 10:51:17