title: Insertion order of multiple records in Entity Framework
body: <p>I'm having trouble with EF reordering my inserts when I try and add an entity with multiple children all at once.  I've got a 3 level structure with one-to-many relationships between each (<code>Outer 1--* Item 1--* SubItem</code>).  If I try and insert a new Outer with Items and Subitems, the Items which contain SubItems end up being inserted first.</p>
  
  <p>Sample Code (.NET 4.5, EF 5.0.0-rc):</p>
  
  <pre><code>public class Outer
  {
      public int OuterId { get; set; }
      public virtual IList&lt;Item&gt; Items { get; set; }
  }
  
  public class Item
  {
      public int OuterId { get; set; }
      [ForeignKey("OuterId")]
      public virtual Outer Outer { get; set; }
  
      public int ItemId { get; set; }
      public int Number { get; set; }
  
      public virtual IList&lt;SubItem&gt; SubItems { get; set; }
  }
  
  public class SubItem
  {
      public int SubItemId { get; set; }
  
      [ForeignKey("ItemId")]
      public virtual Item Item { get; set; }
      public int ItemId { get; set; }
  }
  
  public class MyContext : DbContext
  {
      public DbSet&lt;Outer&gt; Outers { get; set; }
      public DbSet&lt;Item&gt; Items { get; set; }
      public DbSet&lt;SubItem&gt; SubItems { get; set; }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Database.SetInitializer(new DropCreateDatabaseAlways&lt;MyContext&gt;());
          MyContext context = new MyContext();
  
          // Add an Outer object, with 3 Items, the middle one having a subitem
          Outer outer1 = new Outer { Items = new List&lt;Item&gt;() };
          context.Outers.Add(outer1);
          outer1.Items.Add(new Item { Number = 1, SubItems = new List&lt;SubItem&gt;() });
          outer1.Items.Add(new Item { Number = 2, SubItems = new List&lt;SubItem&gt;(new SubItem[] { new SubItem() }) });
          outer1.Items.Add(new Item { Number = 3, SubItems = new List&lt;SubItem&gt;() });
  
          context.SaveChanges();
  
          // Print the order these have ended up in
          foreach (Item item in context.Items)
          {
              Console.WriteLine("{0}\t{1}", item.ItemId, item.Number);
          }
          // Produces output:
          // 1       2
          // 2       1
          // 3       3
      }
  }
  </code></pre>
  
  <p>I'm aware of <a href="https://stackoverflow.com/a/921712/163495">this answer by Alex James</a> which states that inserts may need to be reordered in order to satisfy relational constraints, but that is not the issue here.  His answer also mentions that they can't track the order of items in order-preserving structures such as Lists.</p>
  
  <p>What I'd like to know is how I can get these inserts to be ordered.  While I can rely on sorting my inserted items by a field other than the PK, it's a lot more efficient if I can rely on the PK order.  I don't really want to have to use multiple SaveChanges calls to accomplish this.</p>
  
  <p>I'm using EF5 RC, but judging by the other unanswered questions around, this has been around for some time!</p>