Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop setting JSON array entities' state to Modified in SaveChanges #28926

Merged
merged 1 commit into from
Aug 31, 2022

Conversation

roji
Copy link
Member

@roji roji commented Aug 30, 2022

The issue is when saving entities within JSON arrays. When generating column modifications in SaveChanges, we rebuild the entire JSON document in CreateJson; any entities which are contained in a JSON array get their "ordinal key property" recalculated and assigned (see this line); this changes the entity's state to Modified. Since these entities are unrelated to the original change, we don't accept changes on them (not in entriesToSave in StateManager.SaveChanges), and so they remain Modified after SaveChanges completes.

Note that something similar actually happens when we save a regular entity with a computed property: propagating the computed property marks the entry as Modified. But in that case it's already Modified anyway, and accepting the changes reverts it back to Unchanged.

I tried changing SetStoreGeneratedValue to pass setModified=false to SetStoreGeneratedValue, with the thought that propagating a value back from the database shouldn't change the entity's state; that could even be a slight optimization for regular non-JSON propagation. But that caused test failures in TableSplittingTestBase.Can_use_optional_dependents_with_shared_concurrency_tokens, OptimisticConcurrencySqlServerTestBase.Database_concurrency_token_value_is_updated_for_all_sharing_entities, OptimisticConcurrencySqlServerTestBase.Database_concurrency_token_value_is_updated_for_all_sharing_entities.

So for now I've simply exposed setModified on SetStoreGeneratedValue, which seems minimally-invasive and low risk (but a small public API change. Let me know what you think.

Fixes #28813

@roji roji requested a review from a team August 30, 2022 22:27
@roji roji merged commit c61906a into dotnet:release/7.0 Aug 31, 2022
@roji roji deleted the JsonCollectionIds28813 branch August 31, 2022 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling SaveChanges multiple times sometimes throws with JSON types
2 participants