title: Entity Framework ensure objects get inserted in order
body: <p>I have a table Rules on my database. I insert rules like:</p>
  
  <pre><code>Rule[] rulesToInsert = // some array of rules to insert
  
  using(var db = new MyEntities())
  {
       foreach(var rule in rulesToInsert)
           db.Rules.Add(rule);
       db.SaveChanges();
  }
  </code></pre>
  
  <p><strong>When I retrieve later the rules that I have just added I notice they are in a different order.</strong> What is the best way to retrieve them in the order I added them? Should I call <code>db.SaveChanges()</code> every time I add a new rule? Or should I add a new column called SortOrder? <strong>Why are the items not being added in the order I added them?</strong> </p>
  
  <h2>Edit</h2>
  
  <p>The id is a guid (string) because one rule can have other rules. In other words I am creating a tree structure. (The rules table has a foreign key to itself). It was crashing when I used the primary key as an integer and it autoincremented so I just used a guid instead. I guess I will add a separate column called sort order. </p>