title: How to delete a row in a table and all its foreign key relationships with Entity Framework?
body: <p>I have table called "Categories"<br>
  <a href="https://i.stack.imgur.com/9jjnL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9jjnL.png" alt="enter image description here"></a></p>
  
  <p>Which has a relationship with this table</p>
  
  <p><a href="https://i.stack.imgur.com/xFF8A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xFF8A.png" alt="enter image description here"></a></p>
  
  <p>How can I for a example delete the "ForumTest" category and all the threads related to it in the second table?</p>
  
  <p>EDIT: This is my current code that only works with categories without relationships</p>
  
  <pre><code>[HttpPost]
  public ActionResult DeleteCategory(int? id)
  {
      Category category = db.Categories.Find(id);
  
      db.Threads.RemoveRange(category.Threads);
      db.Categories.Remove(category);
      db.SaveChanges();
  
      return RedirectToAction("Categories");
  }
  </code></pre>