Entity Framework ensure objects get inserted in order

c# entity-framework-6 sql-server

Question

In my database, I have a table called Rules. I add regulations like:

Rule[] rulesToInsert = // some array of rules to insert

using(var db = new MyEntities())
{
     foreach(var rule in rulesToInsert)
         db.Rules.Add(rule);
     db.SaveChanges();
}

The rules I recently inserted are in a different order when I subsequently retrieve them. How do I get them back in the order that I added them? If I should calldb.SaveChanges() each time I introduce a new rule? Alternatively, should I include a new column called SortOrder? Why don't the things appear in the order that I added them?

Edit

Since one rule might have multiple dependent rules, the id is a guid (string). Or to put it another way, I'm building a tree structure. (The rules table is itself foreign keyed.) Using the primary key as an autoincremented integer caused it to crash, thus I switched to using a guid. I suppose I'll include a new column named sort order.

1
3
11/17/2015 8:12:04 PM

Accepted Answer

ZZZ_tmp
-1
11/17/2015 8:13:16 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow