1.常用

db.Entry(实体).State = EntityState.Modified;
db.SaveChanges();

2.指定更新

db.Configuration.ValidateOnSaveEnabled = false;
db.TUser.Attach(实体);
ObjectStateEntry stateEntry = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(实体);
stateEntry.SetModifiedProperty("字段1");
stateEntry.SetModifiedProperty("字段2");
db.SaveChanges();
db.Configuration.ValidateOnSaveEnabled = true;

3.指定更新(推荐!好用!)

TConsultant model = db.TConsultant.FirstOrDefault(t => t.ID == iID);
if (TryUpdateModel(model, new string[] { "Name", "Sex", "PhoneNuber", "CertificateNumber", "Email", "QQ", "Culture" }))
{
          db.SaveChanges();
          return Content("修改成功!");
}

4.代码部分

[HttpPost]
/// <summary>
/// 执行修改的代码
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ActionResult Modify(BlogArticle model)
{
try
{
//1.将实体对象 a.加入 EF 对象容器中,并 b.获取 伪包装类对象
DbEntityEntry<BlogArticle> entry = db.Entry<BlogArticle>(model);
//2.将包装类对象的状态设置为 unchanged
entry.State = System.Data.EntityState.Unchanged;
//3.设置 被改变的属性
entry.Property(a => a.ATitle).IsModified = true;
entry.Property(a => a.AContent).IsModified = true;
entry.Property(a => a.ACate).IsModified = true; //4.提交到数据库 完成修改
db.SaveChanges();
//5.更新成功,则命令浏览器 重定向 到 /Home/List 方法
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return Content("修改失败~~~" + ex.Message);
}
}
升级版:
db.Entry(model).State = EntityState.Unchanged;
db.Entry(model).Property(m => m.字段名).IsModified = true;
db.SaveChanges();

最新文章

  1. C# Byte[] 转String 无损转换
  2. __index
  3. strace追踪未开始或者来不及捕获pid的进程(译)
  4. rdesktop共享剪贴板的问题
  5. 编译c
  6. python 延迟绑定
  7. 【C#】添加引用方式抛出和捕获干净的WebService异常
  8. C#的图像处理方法--(作者:http://conner-wang.spaces.live.com转载)
  9. 0429 Scrum团队成立与第6-7章读后感
  10. Android学习笔记----解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题
  11. 不要让mysql犹豫不决
  12. URI、URL和URN
  13. BZOJ1821: [JSOI2010]Group 部落划分
  14. SVN的版本日期
  15. Shredding Company(dfs)
  16. xml动态修改 dom4j修改
  17. 动态添加试题选项按钮 radioButton(一)
  18. React + Node 单页应用「一」前端搭建
  19. mybatis是如何防止SQL注入的
  20. Spring boot多模块(moudle)中的一个注入错误(Unable to start embedded container; nested exception is org)

热门文章

  1. Linux Install geoip
  2. android GridView 的使用 实现多项选择
  3. NOIP2018退役记
  4. 洛谷 P3237 [HNOI2014]米特运输 解题报告
  5. Python中threading的join和setDaemon的区别及用法
  6. bzoj4336 骑士的旅行 (树链剖分+multiset)
  7. luogu3195/bzoj1010 玩具装箱(斜率优化dp)
  8. MySQL的1067错误解决方法
  9. 面向对象——类的内置attr(三十三)
  10. IDEA进行远程调试