MVC / EF新手…我的MVC Web应用程序是首先使用数据库模型中的代码创建的,因为我已经有了数据库(sqlExpress)…
Store update,insert,or delete statement affected an unexpected
number of rows (0). Entities may have been modified or deleted since
entities were loaded
使用从数据库实体框架模型首先使用代码生成的标准代码时出现此错误.很奇怪,因为我正在使用一台其他人无法访问的独立机器.
这是失败的代码:
if (ModelState.IsValid) { db.Entry(tblPropGroup).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); }
所以我写了下面的工作,一段时间完美无缺:
if (ModelState.IsValid) { db.Entry(_Prop).State = EntityState.Modified; bool saveFailed; do { saveFailed = false; try { db.SaveChanges(); } catch (dbupdateConcurrencyException ex) { saveFailed = true; // Update original values from the database var entry = ex.Entries.Single(); entry.OriginalValues.SetValues(entry.GetDatabaseValues()); } } while (saveFailed);
谁能解释为什么db.SaveChanges在没有其他用户的数据库上导致dbupdateConcurrencyException?如果我对数据进行任何更改,则没有任何区别.我有22个不同的表,我可以在这个程序中访问,如果没有这个解决方法,大约一半不会工作.
解决方法
我怀疑这与数据库上下文相关的竞争条件有关.
以下是一些答案:
side-effect of a feature called optimistic concurrency
Store update,or delete statement affected an unexpected number of rows (0) EntityFramework
这可能有所帮助.
最好使用:
using(DBContext context = new DBContext()) { //Entity code }
处理您的上下文.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。