title: Deleting Children and Parents in MVC4 with EF Codefirst
body: <p>I have some parent/child relationships as follows: </p>
  
  <pre><code>public class Card
  {
      public virtual int CardId { get; set; }
      public virtual Set ParentSet { get; set; }
      public virtual IList&lt;Side&gt; Sides { get; set; }
  }
  
  public class Set
  {
      public virtual int SetId { get; set; }
      public virtual Set ParentSet { get; set; }
      public virtual IList&lt;Card&gt; Cards { get; set; }
  }
  
  public class Side
  {
      public virtual int SideId { get; set; }
      public virtual Card ParentCard { get; set; }
  }
  </code></pre>
  
  <p>So, a Set contains Cards which contain Sides.  A Set can also contain other Sets.  </p>
  
  <p>I tried creating a basic Delete controller action: </p>
  
  <pre><code>public ActionResult DeleteConfirmed(int cardId)
  {
      Card card = _db.Cards.FirstOrDefault(c =&gt; c.CardId == cardId);
      if (card == null)
      {
          return HttpNotFound();
      }
  
      _db.Cards.Remove(card);
      _db.SaveChanges();
      return RedirectToAction("Detail", "Set", new {setId = card.ParentSet.SetId});
  }
  </code></pre>
  
  <p>But I get an error DbUpdateException:</p>
  
  <p>"An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types."</p>
  
  <p>I can post the details here if needed, but I have a feeling this is an issue related to my models or config.  I would imagine that I don't have to build a cascading delete method for every entity that nulls out the parent FKs.... right?  </p>