zoukankan      html  css  js  c++  java
  • 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 = PInfo.GetValue(model, null);
                //        foreach (T us in listModifeding)
                //        {
                //            PInfo.SetValue(us, NewValue, null);
                //        }
                //    }
                //} 
                #endregion
    
                ////return db.SaveChanges();
            }
    EF Modify
  • 相关阅读:
    3574. 乘积数量
    1357. 优质牛肋骨
    1356. 回文质数
    3554. 二进制
    13 vue路由跳转传参
    12 el-form的inline属性
    10 js数组赋值问题
    9 彻底搞懂json字符串和json对象
    8 element自定义卡槽的好处
    7 el-table表格中使用Dropdown 下拉菜单无法显示下拉框的问题
  • 原文地址:https://www.cnblogs.com/valeb/p/6837323.html
Copyright © 2011-2022 走看看