Я пытаюсь asp.net, и я в тупике от System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
. Исключение возникает в строке, в которой я пытаюсь сохранить изменения в моей базе данных в методе действия «Удалить».Как я могу удалить элементы из моей базы данных asp.net
Вот мой код для метода редактирования, который отлично работает:
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? – Velocibadgery
@Aaron Я установил SQL Server Express – user2651804
Возможный дубликат. https://stackoverflow.com/questions/28660708/intermittent-system-data-entity-infrastructure-dbupdateconcurrencyexception –