title: Entity Framework 4 executes stored procedures for insert operation in incorrect order when there is multilevel parent child relationship
body: <p>We are encountering a strange problem with Entity Framework 4. Our model contains an entity which is associated with 3 entities, let us call them first level children. One of the child entity is associated to another entity - let's call this as second level child.</p>
  
  <p>All these 5 entities are mapped to stored procedures for performing insert, update and delete.</p>
  
  <p>Before mapping the stored procedures the order of insert queries issued by entity framework was correct - First main table's insert query is issued, then the insert query for first level children is issued and finally the insert query for second level child is issued.</p>
  
  <p>But after mapping the stored procedures, entity framework is sending the execute stored procedure statements in wrong order - first main table's insert stored procedure followed by insert stored procedures for 2 first level children (which are not parent of second level child), then second level child' stored procedure(s) (for multiple records) and finally the insert stored procedure of remaining first level child which is parent of second level child.</p>
  
  <p>This creates a problem as the second level child's insert before its parent insertion results in foreign key violation.</p>
  
  <p>On removing the stored procedure mapping for table which is parent of second level child, it works without problem.</p>
  
  <p>Anybody has some idea on what determines the order of SQL statements generated by entity framework?</p>