Open divega opened 5 years ago
@divega Interesting - if I understand this correctly, the customer wants to control the order in which items are added to the table, e.g. such that Id
is monotonically increasing with say a TimesheetDate
.
One thing I would do in EF Classic was to do an Interlocked.Increment on the Id column prior to SaveChanges
, usually in the default constructor. I can't remember if this was exactly the same issue and why I did it this way, since this was 6 years ago, but worth mentioning. I recall in my case I would get weird errors about having multiple values in the collection with the same key.
In trying to upgrade to 3.0, this issue has tripped up a number of our integration tests, where we insert a few records in a transaction (some good, some bad), and query them back out to verify our expectations.
The way it works is certainly unexpected -- it seems to consistently insert in reverse order for me.
The quick work around for us is bullet #3 above -- calling SaveChanges
after each .Add()
.
This is based on a dicussion with a customer that is porting a large codebase from LINQ to SQL to EF Core. In the customer's own words:
I discussed this with @AndriySvyryd and although currently the secondary order used is the primary key of the element of the collection, for the insert scenario in which the actual primary key hasn't been generated yet, we could use the collection order instead. That said, at the stage in the update pipeline where we are performing the topological sort, all we have is entity entries and we currently don't have access to the collection, so we it would require some refactoring that makes the feature more expensive.
The main argument for not doing this is that after the initial insertion there is no way to maintain this order, and reliance on the order can easily become a pit of failure. The proper way to achieve this would be to actually implement support for sorted collections as describe in #9067. However implementing this behavior should still be less costly than that.
If we convince ourselves that this is a good behavior to add, we should also discuss if we could implement another behavior that has been asked for in issues like https://github.com/aspnet/EntityFrameworkCore/issues/11686. That one is not about taking into account collection order, but the order in which entities were added to the
DbContext
for inserts with generated keys. It is possible that the implementaiton could be similar.A couple of alternative implementatoins we discussed: