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();
}
 
上一篇:FlexPaper 2.3.6 远程命令执行漏洞 附Exp


下一篇:SSD报告 - QRadar远程命令执行