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();
            }
     
  • 相关阅读:
    修改XCode默认注释并自动生成文档
    百度地图初始化引擎失败
    ios系统分享
    ios判断app是否有打开相机的权限
    mac下https方式连接svn连接不上解决方法
    abbyy ocr sdk
    ant的安装
    ubuntu安装nginx
    ubuntu安装gcc
    iOS保持长时间后台运行
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/10524043.html
Copyright © 2011-2022 走看看