zoukankan      html  css  js  c++  java
  • entity framwork修改指定字段

    1、ef修改时指修改指定字段
    public void ChangePassword(int userId, string password) { var user = new User() { Id = userId, Password = password }; using (var db = new MyEfContextName()) { db.Users.Attach(user); db.Entry(user).Property(x => x.Password).IsModified = true; db.SaveChanges(); } }
    2、ef修改时指定字段不被修改
     public void UpdateCustomer()
            {
    
                // 自己实例化要更新对象 省了访问数据库获取数据时间
                Customer customer3 = new Customer();
                customer3.Id = 22;            
                customer3.CustAddress = "北京25";
                List<string> listRmoveFiled = new List<string>();
                listRmoveFiled = "CustCode,CustName".Split(',').ToList<string>();
    
                CustoemrRepository cuR = new CustoemrRepository();            
                int row = cuR.UpdateModel(customer3,listRmoveFiled);
            }
     /// 修改实体对象
            /// </summary>
            /// <param name="model"></param>
            /// <returns></returns>
            public int UpdateModel(TEntity model, List<string> listRemoveField = null)
            {
    
                // 排除不需更新的字段            
                foreach (string field in listRemoveField)
                {
                    if (field != "Id")
                        unDbContext.Entry(model).Property(field).IsModified = false;
                }
    
                if (unDbContext.Entry<TEntity>(model).State == System.Data.Entity.EntityState.Detached)
                {
                    //将model追加到EF容器                
                    unDbContext.Entry(model).State = EntityState.Modified;
                }
                return unDbContext.SaveChanges();
            }
     
  • 相关阅读:
    2
    3
    尚学堂--网络编程
    尚学堂--线程
    尚学堂--IO
    尚学堂--容器
    谈谈两个来月在小公司的经历
    Dockerfile详解
    docker 升级后或者重装后,启动容器提示:Error response from daemon: Unknown runtime specified docker-runc
    centos7系统优化
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/10524043.html
Copyright © 2011-2022 走看看