title: Entity Framework -- Dependent objects not deleted before principal object when cascade deleting composite key entities
body: <p>I'm having an issue with <strong>cascade deletion</strong> of <strong>dependent objects</strong> with <strong>composite keys</strong>.  Specifically, the emitted SQL instructions by EF tries to delete the principle entity before the dependent entity.</p>
  
  <p>I have <code>OnDelete="Cascade"</code> set in both the CSDL and SSDL of my EF model and am explicitly loading the dependent entities into memory so EF knows to handle them.  I have this working correctly with another FK relationship and EF correctly deletes the children entities before deleting the principle.</p>
  
  <p>However, it's not in this case and I suspect it's because the dependent entity has a Composite Key.  A brief look at the schema:</p>
  
  <pre><code>Entity A              Entity B                 Entity A_B
    ID (PK)               ID (PK)                  A_ID  (PK, FK to A)
                                                   B_ID  (PK, FK to B)
  </code></pre>
  
  <p>I want the entries of Entity A_B to be deleted when I delete Entity A.</p>
  
  <p>I can look into the ObjectStateManager and see A's A_B entities loaded in memory and marked for deletion, yet EF emits a SQL command to delete A <strong>before</strong> deleting its A_Bs.  This works fine when the dependent entity has its own PK, but seems to get tripped up when it's a Composite Key.  It doesn't delete the children first even though I explicitly tell the EF's FK Association (both SSDL and CSDL) that Deletes should be Cascaded.</p>
  
  <p>Again, I have both A and its A_Bs loaded in memory and the ObjectStateManager has them all marked to be deleted.  I'm using POCOs with Proxy Creation enabled, though I explicitly load the child entities in memory via LoadProperty calls against the Context.</p>
  
  <p>Has anyone seen this?  Is the Composite Key really the issue or just a distraction?  Why does EF handle the SQL commands in correct order in one case but not the other?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> I've read the documentation for both OnDelete (<a href="http://msdn.microsoft.com/en-us/library/cc716734.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/cc716734.aspx</a>) and Relationship Management (<a href="http://msdn.microsoft.com/en-us/library/ee373856.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ee373856.aspx</a>).  The section entitled "Considerations for Identifying and Non-identifying Relationships" seems to imply that what I'm wanting to do is possible, and is, indeed, expected.  Could it perhaps be something within the EF provider assembly for the database?  I'm working against an Informix database using IBM.Data.DB2 assembly and NOT against a SQL Server database.</p>
  
  <p>(<strong>Further update:</strong> I have updated to IBM DB2 v9.7fp3a and EF Providers Beta Refresh -- <a href="http://www.ibm.com/developerworks/forums/thread.jspa?threadID=345634&amp;tstart=0" rel="nofollow">http://www.ibm.com/developerworks/forums/thread.jspa?threadID=345634&amp;tstart=0</a>)</p>