Mark entities as unchanged when matched by entities loaded from database#38452
Open
AndriySvyryd wants to merge 3 commits into
Open
Mark entities as unchanged when matched by entities loaded from database#38452AndriySvyryd wants to merge 3 commits into
AndriySvyryd wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes EF Core tracking behavior during explicit loading/tracking queries so that when a query result matches an entity already tracked as Added (same key), the tracked entity is corrected to Unchanged rather than later being re-inserted and causing duplicate-key failures (issue #35762).
Changes:
- Update query identity-resolution (
QueryContext.TryGetEntry) to transition matchingAddedentries toUnchanged. - Add a new SQLite functional test covering the scenario where
Load()matches anAddedentity that already exists in the store. - Tighten specification tests to assert that entities materialized/associated by
Load()are tracked asUnchanged(unless the graph is detached).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/EFCore.Sqlite.FunctionalTests/LoadExistingEntityStateSqliteTest.cs | Adds regression test for explicit-load correcting Added to Unchanged when the entity exists in the database. |
| test/EFCore.Specification.Tests/LoadTestBase.cs | Adds assertions verifying loaded entities end up Unchanged (or Detached when appropriate). |
| src/EFCore/Query/QueryContext.cs | Implements the core behavior change: Added entry matched by a tracking query is set to Unchanged. |
| src/EFCore/Internal/EntityFinder.cs | Removes an unused using. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
cincuranet
approved these changes
Jun 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #35762