Menu Case Study Part 7 – Database Constraints
Prerequisites
Part of Case Study – Menu Example
Summary
Demonstrates how to manage deletion of a menu with foreign key constraints. This video also discusses (briefly) the role of the controller and the DbContext – and also proper programming
Video
Reference Materials
Method Added to MenuDatabaseCOP4709Context.cs
public void DeleteMenu(int id)
{
// All the logic associated with deleting a menu should be in this method
MenuItems.RemoveRange(MenuItems.Where(mi => mi.MenuID == id));
SaveChanges();
Menu menu = Menus.Find(id);
Menus.Remove(menu);
SaveChanges();
}
Changes made to the DeleteConfirmed method in the MenusController.cs
public ActionResult DeleteConfirmed(int id)
{
//Menu menu = db.Menus.Find(id);
//db.Menus.Remove(menu);
//db.SaveChanges();
// Replaced with method in Context
db.DeleteMenu(id);
return RedirectToAction("Index");
}