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
  • 相关阅读:
    LeetCode Generate Parentheses
    MVC中从Controller像View层传值
    IOS_多线程_ASI_AFN_UIWebView
    @PathVariable,@RequestParam, @RequestBody
    sql语句
    连表删除例子
    java中VO的使用(组成复杂的实体类)
    MyBatisPLus入门项目实战各教程目录汇总
    java常用函数
    复杂查询 new EntityWrapper<>()
  • 原文地址:https://www.cnblogs.com/valeb/p/6837323.html
Copyright © 2011-2022 走看看