I have following code in MVC w.r.t. EF 5.0 to delete an Item from a table
Earlier Its working fine. Not willing to use 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? For example let's say an ingredient which has an 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.