Skip to content

Mark entities as unchanged when matched by entities loaded from database#38452

Open
AndriySvyryd wants to merge 3 commits into
mainfrom
Issue35762
Open

Mark entities as unchanged when matched by entities loaded from database#38452
AndriySvyryd wants to merge 3 commits into
mainfrom
Issue35762

Conversation

@AndriySvyryd

Copy link
Copy Markdown
Member

Fixes #35762

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 matching Added entries to Unchanged.
  • Add a new SQLite functional test covering the scenario where Load() matches an Added entity that already exists in the store.
  • Tighten specification tests to assert that entities materialized/associated by Load() are tracked as Unchanged (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.

Comment thread test/EFCore.Specification.Tests/LoadTestBase.cs Outdated
AndriySvyryd and others added 2 commits June 17, 2026 16:54
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 18, 2026 00:27
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 18, 2026 00:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

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.

DbContext.Entry().Reference().Load() falsely tracks existing reference entity as Added

3 participants