title: Entity Framework 6: Code First Cascade delete
body: <p>So there are several similar questions on here to this, but I'm still having issues determing what exactly I'm missing in my simplified scenario.</p>
  
  <p>Let's say I have the following tables, cleverly named after myself: </p>
  
  <pre><code>'JohnsParentTable' (Id, Description) 
  'JohnsChildTable' (Id, JohnsParentTableId, Description)
  </code></pre>
  
  <p>With the resulting classes looking like so</p>
  
  <pre><code>public class JohnsParentTable
  {
      public int Id { get; set; }
      public string Description { get; set; }
      public virtual ICollection&lt;JohnsChildTable&gt; JohnsChildTable { get; set; }
  
      public JohnsParentTable()
      {
          JohnsChildTable = new List&lt;JohnsChildTable&gt;();
      }
  }
  
  internal class JohnsParentTableConfiguration : EntityTypeConfiguration&lt;JohnsParentTable&gt;
  {
      public JohnsParentTableConfiguration()
      {
          ToTable("dbo.JohnsParentTable");
          HasKey(x =&gt; x.Id);
          Property(x =&gt; x.Id).HasColumnName("Id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          Property(x =&gt; x.Description).HasColumnName("Description").IsRequired().HasMaxLength(50);
      }
  }
  
  public class JohnsChildTable
  {
      public int Id { get; set; }
      public string Description { get; set; }
      public int JohnsParentTableId { get; set; }
      public JohnsParentTable JohnsParentTable { get; set; }
  }
  
  internal class JohnsChildTableConfiguration : EntityTypeConfiguration&lt;JohnsChildTable&gt;
  {
      public JohnsChildTableConfiguration()
      {
          ToTable("dbo.JohnsChildTable");
          HasKey(x =&gt; x.Id);
          Property(x =&gt; x.Id).HasColumnName("Id").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          Property(x =&gt; x.Description).HasColumnName("Description").IsRequired().HasMaxLength(50);
          HasRequired(a =&gt; a.JohnsParentTable).WithMany(c =&gt; c.JohnsChildTable).HasForeignKey(a =&gt; a.JohnsParentTableId);
      }
  }
  </code></pre>
  
  <p>In the database I have a row in the parent table with an Id of 1 along with two rows in the child table tied to this parent.  If I do this:</p>
  
  <pre><code>var parent = db.JohnsParentTable.FirstOrDefault(a =&gt; a.Id == 1)
  </code></pre>
  
  <p>The object is correctly populated.  However, if I try to delete this row:</p>
  
  <pre><code>var parent = new Data.Models.JohnsParentTable() { Id = 1 };
  db.JohnsParentTable.Attach(parent);
  db.JohnsParentTable.Remove(parent);
  
  db.SaveChanges();
  </code></pre>
  
  <p>Entity framework tries to execute the following:</p>
  
  <pre><code>DELETE [dbo].[JohnsParentTable]
  WHERE ([Id] = @0)
  -- @0: '1' (Type = Int32)
  -- Executing at 1/23/2014 10:34:01 AM -06:00
  -- Failed in 103 ms with error: The DELETE statement conflicted with the REFERENCE constraint "FK_JohnsChildTable_JohnsParentTable". The conflict occurred in database "mydatabase", table "dbo.JohnsChildTable", column 'JohnsParentTableId'.
  The statement has been terminated.
  </code></pre>
  
  <p>My question then is what exactly am I missing to ensure Entity Framework knows that it must delete the 'JohnsChildTable' rows before deleting the parent?</p>