title: How to delete a record with a foreign key constraint?
body: <p>Started a new ASP.NET MVC 3 application and getting the following error:</p>
  
  <blockquote>
    <p>The primary key value cannot be deleted because references to this key
    still exist.</p>
  </blockquote>
  
  <p>How to solve this?</p>
  
  <p><strong>Models (EF code-first)</strong></p>
  
  <pre><code>public class Journal
  {
      public int JournalId { get; set; }
      public string Name { get; set; }
      public virtual List&lt;JournalEntry&gt; JournalEntries { get; set; }
  }
  public class JournalEntry
  {
      public int JournalEntryId { get; set; }
      public int JournalId { get; set; }
      public string Text { get; set; }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>//
  // POST: /Journal/Delete/5
  
  [HttpPost, ActionName("Delete")]
  public ActionResult DeleteConfirmed(int id)
  {            
      Journal journal = db.Journals.Find(id);
      db.Journals.Remove(journal);
      db.SaveChanges(); // **exception occurs here**
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p><strong>DB Setup</strong></p>
  
  <pre><code>public class FoodJournalEntities : DbContext
  {
      public DbSet&lt;Journal&gt; Journals { get; set; }
      public DbSet&lt;JournalEntry&gt; JournalEntries { get; set; }
  }
  </code></pre>