This project has moved and is read-only. For the latest updates, please go here.

Entities being inserted into database in wrong order


We recently updated to the 1.9 release and are noticing some different behavior. When inserting two or more entities of an EntitySet, the 2nd entity in the list gets inserted into the database first, the 1st entity goes in second, and the rest follow in order. We expect them to be inserted in the order they were added to the EntitySet, like they used to be prior to the upgrade.



After the inserts are done, the database table shows the insert order as: Entity2, Entity1, Entity3, Entity4
The same result happens when using DataContext.EntitySet.CreateNew() instead of DataContext.EntitySet.Add().

In the QuoteDataService.g.cs file, in the call to SaveChanges(datRepository, changeSet, resultSet), changeSet and resultSet show the entities listed in sequential order. The stored procedure used for inserts is just a simple insert, and the same one we’ve been using. The issue seems to be in passing the data from the application to the database. We have tested this with multiple EntitySets.

Do you have any suggestions for inserting into the database in the same order the Entities are listed in the EntitySet?


ChristofSenn wrote Apr 27, 2016 at 5:36 PM

You should not make assumptions on the element order of an EntitySet. It's an IEnumerable and not an IList.
Like a select in an RDBMS doesn't make a guarantee for the ordering of the result set unless you specify an order by clause, you likely should add an OrderBy in your case.

ChristofSenn wrote Apr 27, 2016 at 5:39 PM

...ordering inserts is actually up to EntityFramework.

LorenNicoleBetz wrote Apr 28, 2016 at 3:01 PM

As mentioned, in the QuoteDataService.g.cs file, the call to SaveChanges(datRepository, changeSet, resultSet), shows the changeSet and resultSet with the entities already listed in sequential order (the order they were added). Where would you suggest I add the OrderBy command?

When not using/mapping a stored procedure, the insert order is sequential as expected; but, when using a stored procedure (for every EntitySet with Added Entities) the first two Entities are always inserted in reverse order, but all the remaining Entities are added in order (see previous example). This unusual behavior seems more purposed/coded than accidental/random. Do you know why the behavior differs in the presence or absence of a stored procedure mapping?

I see the dl_InsertSP method calling the Function Import in the Repository.g.cs file, but I can’t find where the method is being called; in fact, I don’t think it’s being called at all because when I comment out the method, inserts are successful. Where is the stored procedure that’s being called located if not in the Repository.g.cs file? Can you point me to the code where the insert stored procedure is being called?

ChristofSenn wrote Dec 27, 2016 at 12:07 PM

Sorry, my comment above doesn't make sense in your context... and sorry for the late response.

I was not aware of this issue.

Since the re-ordering only happens when mapping stored procs, I presume there are no relational dependencies e.g. between Entity1 and Entity2 or other entities that could influence this behavior?

I'm not exactly sure what the dl_InsertSP and the Function Import in the Repository.g.cs file are.
However, I want to point out, that actual calls to the database are always done by the Entity Framework. N-Tier Entity Framework never initiates DB calls directly. So, if in doubt whether an issue originates from ntieref or ef, you can always try to reproduce an issue by using Entity Framework directly.