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();
            }
     
  • 相关阅读:
    CSS使用规则总结
    python虚拟机内存泄露?
    对象内存池
    由引擎接口自顶向下的设计引擎结构
    【译】Lesson 0: 开始学习WebGL
    【译】Lesson 1: 一个三角形和一个方块
    网盘中搭建git服务
    行为树(Behavior Tree)
    显卡参数大全
    VTune 备忘
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/10524043.html
Copyright © 2011-2022 走看看