title: Cascade on delete using EF Code First
body: <p>I'm developing an Entity Framework Code First 6.1.3 library with C# and .NET Framework 4.5.1.</p>
  
  <p>I have these three tables:
  <img src="https://i.stack.imgur.com/sGCRR.png" alt="enter image description here"></p>
  
  <p>And these there <code>EntityTypeConfiguration</code> files:
  Class <code>CHILDSConfiguration</code>:</p>
  
  <pre><code>class AGGREGATION_CHILDSConfiguration : EntityTypeConfiguration&lt;AGGREGATION_CHILDS&gt;
  {
      public AGGREGATION_CHILDSConfiguration()
      {
          HasKey(ag_ch =&gt; ag_ch.CHILD_CODE);
  
          Property(ag_ch =&gt; ag_ch.CHILD_CODE)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          Property(ag_ch =&gt; ag_ch.CHILD_CODE)
              .HasMaxLength(20)
              .IsRequired()
              .HasColumnName("CODE");
  
          Property(ag_ch =&gt; ag_ch.PARENT_CODE)
              .HasMaxLength(20)
              .IsRequired();
  
          HasRequired(ag_ch =&gt; ag_ch.Aggregation)
              .WithMany(ag =&gt; ag.AggregationChilds)
              .HasForeignKey(ag_ch =&gt; ag_ch.PARENT_CODE);
  
          HasRequired(ag_ch =&gt; ag_ch.Code)
              .WithOptional(c =&gt; c.AggregationChild)
              .WillCascadeOnDelete(false);
      }
  }
  </code></pre>
  
  <p>Class <code>AGGREGATIONSConfiguration</code>:</p>
  
  <pre><code>class AGGREGATIONSConfiguration : EntityTypeConfiguration&lt;AGGREGATIONS&gt;
  {
      public AGGREGATIONSConfiguration()
      {
          HasKey(ag =&gt; ag.PARENT_CODE);
  
          Property(ag =&gt; ag.PARENT_CODE)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          Property(ag =&gt; ag.PARENT_CODE)
              .HasMaxLength(20)
              .IsRequired()
              .HasColumnName("CODE");
  
          Property(ag =&gt; ag.CREATED)
              .HasMaxLength(50)
              .IsOptional();
  
          HasRequired(ag =&gt; ag.Code)
              .WithOptional(c =&gt; c.Aggregation)
              .WillCascadeOnDelete(false);
      }
  }
  </code></pre>
  
  <p>Class <code>CODESConfiguration</code>:</p>
  
  <pre><code>class CODESConfiguration : EntityTypeConfiguration&lt;CODES&gt;
  {
      public CODESConfiguration()
      {
          HasKey(c =&gt; c.CODE);
  
          Property(c =&gt; c.CODE)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          Property(c =&gt; c.CODE)
              .HasMaxLength(20);
  
          Property(c =&gt; c.CODE_LEVEL)
              .IsRequired();
  
          Property(c =&gt; c.COMMISIONING_FLAG)
              .IsRequired();
  
          Property(c =&gt; c.IS_TRANSMITTED)
              .IsRequired();
  
          Property(c =&gt; c.TIMESPAN)
              .HasMaxLength(50)
              .IsOptional();
  
          Property(c =&gt; c.USERNAME)
              .HasMaxLength(50)
              .IsOptional();
  
          Property(c =&gt; c.SOURCE)
              .HasMaxLength(50)
              .IsOptional();
  
          Property(c =&gt; c.REASON)
              .HasMaxLength(200)
              .IsOptional();
  
          Property(c =&gt; c.ALTERNATE_CODE)
              .IsOptional();
  
          Property(c =&gt; c.HELPER_CODE)
              .IsOptional();
      }
  }
  </code></pre>
  
  <p>I want to do this: if I delete a row on <code>CODES</code> table, I want to delete that code on <code>AGGREGATIONS</code> and on <code>AGGREGATION_CHILDS</code> table.</p>
  
  <p>How can I do that using Entity Framework Code First?</p>
  
  <p>I've had to add <code>.WillCascadeOnDelete(false);</code> because I get an error about cycles and multiple cascade routes.</p>