如何解决asp.net-mvc无其他用户DB上DbUpdateConcurrencyException失败问题?

2026-03-30 11:341阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计431个文字,预计阅读时间需要2分钟。

如何解决asp.net-mvc无其他用户DB上DbUpdateConcurrencyException失败问题?

MVC+EF新手教程:我的MVC Web应用程序首先使用数据库模型中的代码创建,因为我已经有了数据库(SQLExpress)。尝试将记录更新到数据库时,收到以下错误:'Store update, insert, or delete'。

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, insert, or delete statement affected an unexpected number of rows (0) EntityFramework

这可能有所帮助.

最好使用:

using(DBContext context = new DBContext()) { //Entity code }

处理您的上下文.

如何解决asp.net-mvc无其他用户DB上DbUpdateConcurrencyException失败问题?

标签:MV

本文共计431个文字,预计阅读时间需要2分钟。

如何解决asp.net-mvc无其他用户DB上DbUpdateConcurrencyException失败问题?

MVC+EF新手教程:我的MVC Web应用程序首先使用数据库模型中的代码创建,因为我已经有了数据库(SQLExpress)。尝试将记录更新到数据库时,收到以下错误:'Store update, insert, or delete'。

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, insert, or delete statement affected an unexpected number of rows (0) EntityFramework

这可能有所帮助.

最好使用:

using(DBContext context = new DBContext()) { //Entity code }

处理您的上下文.

如何解决asp.net-mvc无其他用户DB上DbUpdateConcurrencyException失败问题?

标签:MV