Skip to content

Fix owned entity data loss when moving between parent collections#37693

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/port-pr-36768-to-main
Draft

Fix owned entity data loss when moving between parent collections#37693
Copilot wants to merge 7 commits intomainfrom
copilot/port-pr-36768-to-main

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

  • Apply fix to ChangeDetector.cs - allow key changes for owned entities in collection navigations
  • Apply fix to NavigationFixer.cs - treat owned entity moves as Added state when FK is part of PK
  • Apply fix to InternalEntryBase.cs - allow key modification for owned collection entities
  • Extract IsOwnedCollectionForeignKey extension method in PropertyExtensions.cs (used by both ChangeDetector and InternalEntryBase)
  • Reset store-generated key values in PrepareForAdd when re-parenting owned collection entities (fixes SQL Server IDENTITY_INSERT error)
  • Move SaveChanges test to GraphUpdatesTestBaseMiscellaneous.cs (SpecificationTests)
  • Update existing tests that expected InvalidOperationException for owned entity collection parent changes
  • Fix OptionalDependentWithoutIdentifyingPropertyWarning by marking NestedOwnedData.Value as required
  • Handle ForceClientNoAction case in test (expect DbUpdateException)
  • Skip test on SQLite (composite key with value generation not supported)
  • All 523 OwnedFixupTest pass, 3097 ChangeTracking pass, 5265 InMemory GraphUpdates pass, 8994 SQLite GraphUpdates pass

💡 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 3 commits February 13, 2026 05:14
…lections

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…ctions

Apply changes from PR #36768 to main branch:
- ChangeDetector.ThrowIfKeyChanged: Allow FK changes for owned entities in collection navigations
- NavigationFixer.UndeleteDependent: Set state to Added when FK overlaps PK in owned collection
- InternalEntryBase.SetPropertyModified: Allow key modification for owned collection entities
- Add test: Nullable_owned_entity_data_is_preserved_when_moving_between_parents
- Update existing tests that previously expected InvalidOperationException

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Port PR 36768 to main and add requested test Fix owned entity data loss when moving between parent collections Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 05:36
…ecificationTests

Address review feedback:
- Extract shared IsOwnedCollectionForeignKey extension method in PropertyExtensions
- Use it as a single-line condition in both ChangeDetector and InternalEntryBase
- Move SaveChanges test from OwnedFixupTest to GraphUpdatesTestBaseMiscellaneous
- Add model classes to GraphUpdatesTestBase

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
… ForceClientNoAction, skip SQLite composite key test

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
#region Issue36206

[ConditionalTheory, InlineData(false), InlineData(true)] // Issue #36206
public virtual async Task Owned_entity_data_is_preserved_when_moving_between_parent_collections(bool async)
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Fix error in GraphUpdatesSqlServerClientCascadeTest:
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while saving the entity changes. See the inner exception for details.
---- Microsoft.Data.SqlClient.SqlException : Cannot insert explicit value for identity column in table 'OwnedEntityWithNestedOwned' when IDENTITY_INSERT is set to OFF.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ef4e6d4. In PrepareForAdd, when an owned collection entity transitions from Deleted to Added (re-parenting), store-generated non-FK key properties are now reset to their sentinel values. This allows value generators to produce new values on insert, preventing the SQL Server IDENTITY_INSERT error.

All 5265 InMemory GraphUpdates, 8994 SQLite GraphUpdates, 3097 ChangeTracking, and 523 OwnedFixupTest tests pass.

…ntities

In PrepareForAdd, when transitioning from Deleted to Added for owned
collection entities, reset store-generated non-FK key properties to their
sentinel values. This allows value generators to produce new values on
insert, fixing the SQL Server IDENTITY_INSERT error.

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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.

2 participants