magritton
6/22/2017 - 2:52 PM

Example of a cascade delete with the entity framework and the database first model using foreign keys.

Example of a cascade delete with the entity framework and the database first model using foreign keys.

public ActionResult DeleteConfirmed(int id)
        {
            SavedQuery savedQuery = db.SavedQueries.Find(id);
            db.SavedQueryFields.RemoveRange(savedQuery.SavedQueryFields);
            db.SavedQueryCriterias.RemoveRange(savedQuery.SavedQueryCriterias);
            db.SavedQueries.Remove(savedQuery);
            db.SaveChanges();

            return RedirectToAction("Index");
        }