[release/9.0] Take into account store-generated values when ordering update commands #34626
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #33023
Description
EF orders update commands in a way that avoids breaking store constraints, like unique indexes.
E.g. if there's a unique index on a
Name
column and there's currently a row withJared
and a row withSam
. The user wants to update the first row toSam
and the second row toArtak
. EF needs perform the second update first.This gets complicated when the index is over a column with a store-computed value since EF cannot predict the value it will have after an update. A workaround is to duplicate the store logic in the C# implementation. This client-side value would never be sent to the store but could be used to determine the update order. However, this workaround was broken because we also needed to avoid dependency cycles in the updates that would prevent establishing an order when a row with the same value is deleted and inserted. The way we handled this was by collapsing the delete and insert into an update and ignoring the store-generated values for ordering purposes.
This change to the ordering logic uses more fine-grained approach for determining whether a store-generated value could affect the ordering that works for both scenarios.
Customer impact
For models with a unique index over a computed column the update order can no longer be correctly determined and will throw an exception if it doesn't happen to be correct. The workaround is to send updates to the affected table one-by-one, but this would have a negative perf impact.
How found
Customer reported
Regression
Yes, from 6.0.x
Testing
Tests added.
Risk
Medium. While this code has good test coverage, due to its nature it's hard to predict all the ramifications of this change.