Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix NRE in FindJsonPartialUpdateInfo when replacing TPH derived entity with owned JSON#37823

Merged
AndriySvyryd merged 8 commits intomainfrom
copilot/fix-nullreferenceexception-tph-inheritance
Mar 3, 2026
Merged

Fix NRE in FindJsonPartialUpdateInfo when replacing TPH derived entity with owned JSON#37823
AndriySvyryd merged 8 commits intomainfrom
copilot/fix-nullreferenceexception-tph-inheritance

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

Fixes #36019

Fixes a NullReferenceException in ModificationCommand.FindJsonPartialUpdateInfo when a TPH derived entity with owned JSON columns is removed and replaced by a base entity using the same primary key.

When replacing a JsonEntityInheritanceDerived with a JsonEntityInheritanceBase sharing the same key, the deleted derived entity's JSON-owned dependents (e.g. ReferenceOnDerived, CollectionOnDerived) still appear as modified entries. During partial-update path discovery, FindPrincipal returns null for these orphaned JSON entries because their principal no longer exists in the change tracker—the new base entity doesn't have those derived-only navigations. The code previously assumed FindPrincipal would always return non-null, causing the NRE.

Fix: Made currentEntry nullable inside FindJsonPartialUpdateInfo, removed the null-forgiving operator from the FindPrincipal call, and added explicit null handling in the loop condition and null-coalescing downstream. When FindPrincipal returns null, the method now returns null (skipping JSON partial update for that entry), which allows the update pipeline to proceed correctly.

Test: Added Replace_derived_entity_with_json_to_base_entity_with_same_key regression test using the existing JsonEntityInheritanceBase/JsonEntityInheritanceDerived hierarchy, with SQL baseline assertion for SQL Server.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits March 2, 2026 19:41
…placing derived entity (#36019)

Add test `Replace_derived_entity_with_json_to_different_derived_type_with_same_key` that
reproduces NullReferenceException when removing a derived TPH entity with an owned JSON type
and adding a different derived entity without JSON but with the same composite key.

Fix: In `FindJsonPartialUpdateInfo`, check if `FindPrincipal` returns null (which happens when
the principal entity was replaced with a different derived type in TPH) and return null to skip
JSON-specific processing, consistent with the existing pattern for deleted parent entities.

Co-authored-by: AndriySvyryd <[email protected]>
Copilot AI changed the title [WIP] Fix NullReferenceException in TPH inheritance with owned JSON type Fix NRE in FindJsonPartialUpdateInfo when replacing TPH derived entity with owned JSON Mar 2, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a NullReferenceException in EF Core’s relational update pipeline when a TPH derived entity with owned JSON dependents is deleted and replaced by a base (or different derived) entity using the same key, causing JSON-owned dependents to lose their principal during partial-update path discovery.

Changes:

  • Added a null guard in ModificationCommand.FindJsonPartialUpdateInfo to handle FindPrincipal(...) returning null.
  • Added a relational specification test covering “replace derived-with-JSON → base with same key”.
  • Hooked the new test into the SQL Server JSON type functional test suite (override to satisfy the “all tests overridden” check).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/EFCore.Relational/Update/ModificationCommand.cs Prevents NRE by handling null principals when walking up JSON-owned entry ownership chain.
test/EFCore.Relational.Specification.Tests/Update/JsonUpdateTestBase.cs Adds regression test reproducing the TPH replacement scenario with derived-only JSON navigations.
test/EFCore.SqlServer.FunctionalTests/Update/JsonUpdateJsonTypeSqlServerTest.cs Overrides the new base test for the SQL Server JSON type suite (consistent with existing override coverage checks).

…tity_with_same_key in JsonUpdateJsonTypeSqlServerTest

Co-authored-by: AndriySvyryd <[email protected]>
@AndriySvyryd
Copy link
Member

@copilot Update description with a nice summary of the changes

Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot Update description with a nice summary of the changes

Updated the PR description with a summary covering the root cause (orphaned JSON entries from derived-only navigations after TPH entity replacement), the fix (nullable currentEntry with explicit null handling in FindJsonPartialUpdateInfo), and the regression test.

@AndriySvyryd AndriySvyryd marked this pull request as ready for review March 3, 2026 01:01
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner March 3, 2026 01:01
Copilot AI review requested due to automatic review settings March 3, 2026 01:01
Copy link

Copilot AI left a comment

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.

@AndriySvyryd AndriySvyryd enabled auto-merge (squash) March 3, 2026 07:52
@AndriySvyryd AndriySvyryd merged commit 41811eb into main Mar 3, 2026
18 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/fix-nullreferenceexception-tph-inheritance branch March 3, 2026 08:29
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.

TPH inheritance with owned JSON type causes NullReferenceException when replacing derived entity with same key

4 participants