title: Entity Framework cascading delete problems - Foreign key set to null
body: <p>I have the following Model which I mapped with the Entity Framework:
  Mitglied -> Auftrag -> Teilprojekt</p>
  
  <p>I have set up everything in the database with foreign keys and "on delete cascade". If I perform some tests on the database everything works fine. The problem arises as soon as I use the Entity Framework to add and especially delete objects.
  Consider the following code:</p>
  
  <pre><code>Mitglieder m1 = new Mitglieder();
  m1.Name = "erstes";
  
  Auftraege a1 = new Auftraege();
  a1.Name = "a1";
  m1.Auftraege.Add(a1);
  
  Teilprojekte t1 = new Teilprojekte();
  t1.Name = "t1";
  a1.Teilprojekte.Add(t1);
  
  context.AddToMitglieder(m1);
  
  
  Mitglieder m2 = new Mitglieder();
  m2.Name = "zweites";
  
  Auftraege a2 = new Auftraege();
  a2.Name = "a2";
  m2.Auftraege.Add(a2);
  
  Teilprojekte t2 = new Teilprojekte();
  t2.Name = "t2";
  a2.Teilprojekte.Add(t2);
  
  context.AddToMitglieder(m2);
  context.SaveChanges();
  </code></pre>
  
  <p>This adds correctly all the objects and sets the foreign keys. If I then execute the following code to delete the Auftraege:</p>
  
  <pre><code> var members = context.Mitglieder.ToList();
  
   var mem1 = members.Single(m =&gt; m.Name == "erstes");
   mem1.Auftraege.Load();
   var auf1 = mem1.Auftraege.First();
   context.DeleteObject(auf1);
  
   var mem2 = members.Single(m =&gt; m.Name == "zweites");
   mem2.Auftraege.Load();
   var auf2 = mem2.Auftraege.First();
  
   //THIS IS THE LINE THAT MAKES THE DIFFERENCE
   auf2.Teilprojekte.Load();
  
   context.DeleteObject(auf2);
  
   context.SaveChanges();
  </code></pre>
  
  <p>The two Auftraege a1 and a2 are correctly deleted; also the Teilprojekt t1 is deleted but the Teilprojekt t2 is not deleted;</p>
  
  <p><strong>Its AuftragID column is set to NULL!</strong></p>
  
  <p>The only difference is that for a1 I did not load the Teilprojekte and for a2 I did load them. I don't understand why this is not working properly. Also in the SSDL everything is set up correctly:</p>
  
  <pre><code>    &lt;Association Name="FK_Auftraege_Mitglieder"&gt;
        &lt;End Role="Mitglieder" Type="TechnBuero.Store.Mitglieder" Multiplicity="0..1"&gt;
          &lt;OnDelete Action="Cascade" /&gt;
        &lt;/End&gt;
        &lt;End Role="Auftraege" Type="TechnBuero.Store.Auftraege" Multiplicity="*" /&gt;
        &lt;ReferentialConstraint&gt;
          &lt;Principal Role="Mitglieder"&gt;
            &lt;PropertyRef Name="ID" /&gt;
          &lt;/Principal&gt;
          &lt;Dependent Role="Auftraege"&gt;
            &lt;PropertyRef Name="Mitglieder_ID" /&gt;
          &lt;/Dependent&gt;
        &lt;/ReferentialConstraint&gt;
      &lt;/Association&gt;
      &lt;/Association&gt;
          &lt;Association Name="FK_Teilprojekte_Auftraege"&gt;
        &lt;End Role="Auftraege" Type="TechnBuero.Store.Auftraege" Multiplicity="0..1"&gt;
          &lt;OnDelete Action="Cascade" /&gt;
        &lt;/End&gt;
        &lt;End Role="Teilprojekte" Type="TechnBuero.Store.Teilprojekte" Multiplicity="*" /&gt;
        &lt;ReferentialConstraint&gt;
          &lt;Principal Role="Auftraege"&gt;
            &lt;PropertyRef Name="ID" /&gt;
          &lt;/Principal&gt;
          &lt;Dependent Role="Teilprojekte"&gt;
            &lt;PropertyRef Name="AuftragsID" /&gt;
          &lt;/Dependent&gt;
        &lt;/ReferentialConstraint&gt;
      &lt;/Association&gt;
  </code></pre>
  
  <p>If I set the AuftragsID on the table Teilprojekte to not null then I get the following error from the Entity Framework:</p>
  
  <pre><code>Error 1 Error 3023: Problem in Mapping Fragments starting at lines 638, 702: Column Teilprojekte.AuftragsID has no default value and is not nullable. A column value is required to store entity data. 
  An Entity with Key (PK) will not round-trip when:
  ((PK is in 'Teilprojekte' EntitySet OR PK plays Role 'Teilprojekte' in AssociationSet 'FK_Teilprojekte_Auftraege') AND (PK is NOT in 'Teilprojekte' EntitySet OR PK does NOT play Role 'Teilprojekte' in AssociationSet 'FK_Teilprojekte_Auftraege'))
  </code></pre>
  
  <p>Can anyone help me and tell me what is going wrong and how I can solve this issue?
  Thanks</p>