简体   繁体   English

删除记录时出现 System.Data.Entity.Infrastructure.DbUpdateException

[英]System.Data.Entity.Infrastructure.DbUpdateException while Deleting Record

I have following code in MVC w.r.t.我在 MVC w.r.t 中有以下代码。 EF 5.0 to delete an Item from a table EF 5.0 从表中删除项目

Earlier Its working fine.早些时候它工作正常。 Not willing to use try...catch不愿意使用 try...catch

[HttpPost, ActionName("Delete")]
  public ActionResult DeleteConfirmed(int id)
  {
     using (OrderDBContext db = new OrderDBContext())
     {
        FoodItem food = db.FoodItems.Find(id);
        db.FoodItems.Remove(food);
        db.SaveChanges(); //Exception
     }
     return RedirectToAction("Index");
  }

It should delete the record and redirect to "Index"它应该删除记录并重定向到“索引”

Could it be you have a related entity with your FoodItemId as a foreign key?难道你有一个相关的实体,你的 FoodItemId 作为外键? For example let's say an ingredient which has an FoodItemId.例如,假设有一个 FoodItemId 的成分。 That way you wouldn't be able to delete the fooditems before deleting all the ingredients unless you have a cascading delete set on your tables.这样,除非您在表上设置了级联删除,否则您将无法在删除所有成分之前删除食品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue 发生System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException occurred MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException C#System.Data.Entity.Infrastructure.DbUpdateException插入新记录 - c# System.Data.Entity.Infrastructure.DbUpdateException inserting a new record 如何解决 System.Data.Entity.Infrastructure.DbUpdateException - How to solve System.Data.Entity.Infrastructure.DbUpdateException 递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException) EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 如何避免System.Data.Entity.Infrastructure.DbUpdateException - How to avoid System.Data.Entity.Infrastructure.DbUpdateException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM