Ordering of Commands – What do you think?

Some of our customers write code like this:


And expect the Entity Framework to issue 3 insert commands in the same order.

Today the EF doesn’t preserve this sort of ordering.

Instead the order inserts/updates and deletes are made in the Entity Framework is based mainly on local inter-dependencies.

For example if you insert a new Product in a new Category we have to add the Category before the Product .

This means that if you have a large set of changes there are local ordering constraints that we must satisfy first, and these rules can easily be in conflict with order things actually happen in the ObjectContext.

For example if you do this:

   new Product{
      Name = "Bovril",
      Category = new Category {Name = "Food"}

the effect is that the Product is added (to the context) first and then when we walk the graph we add the Category too.

i.e. the insert order into the context is:


but because of referential integrity constraints we must re-order like this before attempting to insert into the database:


So this kind of local re-ordering is kind of non-negotiable.

However if there are no local dependencies like this, you could in theory preserve the order of changes.

Today though we don't track 'when' something was added to the context or even when something gets modified, and for efficiency reason we don't track entities in order preserving structures like lists. So currently we can't preserve the order of unrelated inserts, updates and deletes etc.

My question to you is how important is this?

Would you like to see us preserve your ordering as much as possible?

Comments (5)

  1. Wojciech Gebczyk says:

    Hmm… The only "resonable" reason that defining object dependency would be valuable is when there are constraints on DB that cannot be expressed in EDMX because of some reasons (partial mapping? procedures? triggers?). In such case having method on object/context like obj1.DependsOn(obj2) will be sufficient.


  2. Alex D James says:


    I like your obj1.DependsOn(obj2) idea.

    very interesting


  3. Vijay says:

    we need ordering!!

    Keep it simple.

  4. Alex D James says:

    Thanks for the feedback Vijay

  5. DaveR says:

    Can’t think of a single reason why ordering would be important.  EF is a way of hiding the complexities of the storage mechanism, so why would I want to use EF if I have specific storage-mechanism requirements?

    FWIW I think you’re right to not bother preserving order.

Skip to main content