【发布时间】:2016-04-22 01:39:20
【问题描述】:
我正在试用 asp.net,但我被 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException 难住了。异常发生在我尝试在“删除”操作方法中将更改保存到我的数据库的行。
这是我的编辑方法代码,效果很好:
public ActionResult Edit(int id)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
[HttpPost]
public ActionResult Edit(Movie movie)
{
db.Entry(movie).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
这是我的删除方法的代码,它没有!
public ActionResult Delete(int id)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
[HttpPost]
public ActionResult Delete(Movie movie)
{
db.Movies.Attach(movie);
db.Movies.Remove(movie);
//db.Entry(movie).State = System.Data.Entity.EntityState.Deleted;
//both the outcommented line above and the current method of marking for deletion result in the same exception on the line below.
db.SaveChanges();
return RedirectToAction("Index");
}
【问题讨论】:
-
了解数据库类型会有所帮助:mysql、sqlserver、dbase?
-
@Aaron 我安装了 SQL Server Express
-
ASP.NET 是 web 框架 - SQL Server (Express) 是数据库 ....
标签: c# asp.net asp.net-mvc sql-server-express