Order of Insertion in EF 6 RRS feed

  • Question

  • In EF6, how can i maintain the same order of insertion? I came across different posts saying that EF6 changes the order while inserting, e.g. but i am unable to understand the reason behind it. Even if this is the case how can we force to keep the same order as it is important in some cases. For examples i am trying to record rules that should be executed in sequence. Now after retriving the rules back my sequence of execution changes due to random insertion by EF6.
    Monday, December 31, 2018 7:04 PM

All replies

  • Even the link says that the developer can do nothing about this.

    If this is the case that you need to do something that is beyond the capabilities of EF6, then go to the EF backdoor,  use ADO.NET, MS SQL Server Command Objects, inline T-SQL or a stored procedure.
    The point of the below link is how to use the IObjectContextAdapter in EF6, becuase all you want is the connection through EF6 so that you can start using the MS SQL Server Command Objects and use ADO.NET without EF being involved for CRUD with the underlying DB technology.

    Monday, December 31, 2018 8:09 PM
  • Hi Furqan.Safdar,

    I think it is a by design issue, there is no guarantee that entities will be inserted in the order that they are tracked. There are actually many reasons for this, both around efficiency of internal data structures and the need to re-order where dependencies exist. Also, many stores will not preserve order once inserted, and hence trying to preserve order while inserting doesn't help.

    You could control the order in your example by setting key values on your entities, though those key values will be overridden in the database.

    context.Add(New Person { ID = 1, Name = "1st"});
    context.Add(New Person { ID = 2, Name = "2nd"});

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, January 1, 2019 1:59 AM