Fix owned entity data loss when moving between parent collections#37693
Fix owned entity data loss when moving between parent collections#37693
Conversation
…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>
…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>
test/EFCore.Specification.Tests/GraphUpdates/GraphUpdatesTestBase.cs
Outdated
Show resolved
Hide resolved
… 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) |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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>
ChangeDetector.cs- allow key changes for owned entities in collection navigationsNavigationFixer.cs- treat owned entity moves as Added state when FK is part of PKInternalEntryBase.cs- allow key modification for owned collection entitiesIsOwnedCollectionForeignKeyextension method inPropertyExtensions.cs(used by both ChangeDetector and InternalEntryBase)PrepareForAddwhen re-parenting owned collection entities (fixes SQL Server IDENTITY_INSERT error)GraphUpdatesTestBaseMiscellaneous.cs(SpecificationTests)OptionalDependentWithoutIdentifyingPropertyWarningby markingNestedOwnedData.Valueas requiredForceClientNoActioncase in test (expectDbUpdateException)💡 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.