Fix compiled-model CreateRelationalModel() failure when an owned entity shares its owner's table#38428
Open
Copilot wants to merge 6 commits into
Open
Fix compiled-model CreateRelationalModel() failure when an owned entity shares its owner's table#38428Copilot wants to merge 6 commits into
Copilot wants to merge 6 commits into
Conversation
…owner's table Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix CreateRelationalModel() to handle owned entities
Fix compiled model CreateRelationalModel() failure with owned entity sharing owner's table
Jun 13, 2026
There was a problem hiding this comment.
Pull request overview
Fixes a runtime failure when creating the relational model from a compiled model in scenarios where an owned entity shares its owner’s table and the owner has an index/constraint. The fix ensures table constraints/indexes/triggers are generated only after all column mappings exist, preventing empty-sequence failures during value-factory setup.
Changes:
- Updated
RelationalCSharpRuntimeAnnotationCodeGeneratorto generate unique constraints, indexes, and triggers in a dedicated post-pass overmodel.Tables, after all column mappings are emitted (and before FK constraints). - Added a regression test (
Owned_entity_sharing_table_with_owner_that_has_an_index) to force relational-model creation in the problematic mapping order and validate index presence. - Regenerated/updated compiled-model scaffolding baselines (primarily reordering constraint/index emission after mappings) and added new Sqlite/SqlServer baselines for the new test.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs | Moves table constraint/index/trigger emission to a table-level post-pass after column mappings are generated. |
| test/EFCore.Relational.Specification.Tests/Scaffolding/CompiledModelRelationalTestBase.cs | Adds regression test and supporting Blog/Post/Author types for the owned-shared-table + index scenario. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Tpc_Sprocs/DbContextModelBuilder.cs | Baseline update reflecting reordered constraint/index emission. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextModelBuilder.cs | Baseline update reflecting reordered constraint/index emission. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextModelBuilder.cs | Baseline update reflecting reordered constraint/index emission. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextModelBuilder.cs | Baseline update reflecting reordered constraint/index emission. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextAssemblyAttributes.cs | New baseline assembly attributes for the SqlServer provider. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextModel.cs | New baseline runtime model for the SqlServer provider. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextModelBuilder.cs | New baseline relational model builder output for the SqlServer provider. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/BlogEntityType.cs | New baseline entity type for Blog (SqlServer). |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/BlogUnsafeAccessors.cs | New baseline unsafe accessors for Blog (SqlServer). |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/PostEntityType.cs | New baseline entity type for Post (SqlServer). |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/PostUnsafeAccessors.cs | New baseline unsafe accessors for Post (SqlServer). |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/AuthorEntityType.cs | New baseline entity type for owned Author (SqlServer). |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/AuthorUnsafeAccessors.cs | New baseline unsafe accessors for Author (SqlServer). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextAssemblyAttributes.cs | New baseline assembly attributes for the Sqlite provider. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextModel.cs | New baseline runtime model for the Sqlite provider. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/DbContextModelBuilder.cs | New baseline relational model builder output for the Sqlite provider. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/BlogEntityType.cs | New baseline entity type for Blog (Sqlite). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/BlogUnsafeAccessors.cs | New baseline unsafe accessors for Blog (Sqlite). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/PostEntityType.cs | New baseline entity type for Post (Sqlite). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/PostUnsafeAccessors.cs | New baseline unsafe accessors for Post (Sqlite). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/AuthorEntityType.cs | New baseline entity type for owned Author (Sqlite). |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Owned_entity_sharing_table_with_owner_that_has_an_index/AuthorUnsafeAccessors.cs | New baseline unsafe accessors for Author (Sqlite). |
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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
Fix compiled model CreateRelationalModel() failure with owned entity sharing owner's table
Fix compiled-model CreateRelationalModel() failure when an owned entity shares its owner's table
Jun 16, 2026
…atory comment Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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 #37981
A compiled model whose owned entity shares its owner's table (where the owner has an index/constraint) threw
System.InvalidOperationException: Sequence contains no elementsfromCreateRelationalModel()at runtime, despite compiling successfully.Root cause
RelationalCSharpRuntimeAnnotationCodeGeneratoremitted unique-constraint/index/trigger creation inline, guarded bytableMapping == table.EntityTypeMappings.Last(). When an owned entity (e.g.Author) shares its owner's (Post) table and its mapping is the lastEntityTypeMappingbut is iterated before the owner, an index (e.g.IX_Posts_BlogId) was generated before the referenced column's mapping existed.SetRowIndexValueFactory(...)then hitColumnBase.GetDefaultStoreTypeMapping(), which requires at least one column mapping — hence the empty-sequence failure.Changes
RelationalCSharpRuntimeAnnotationCodeGenerator: moved unique-constraint, index, and trigger emission out of the per-ITableMappingpath into aCreateTableConstraintspass overmodel.Tables, run after all column mappings are emitted, guaranteeing every referenced column is mapped before constraints/indexes are built.model.Tablespass afterCreateTableConstraints. Generated FKs resolve their principal's unique constraint at runtime viaprincipalTable.FindUniqueConstraint(name)!, andIRelationalModel.Tablesis not topologically ordered — so all unique constraints must be emitted before any FK. TheforeignKeysOnlyparameter was removed; the FK loop is inlined.Owned_entity_sharing_table_with_owner_that_has_an_indexregression test (Blog/Post/Author).BigModel_with_JSON_columns,ComplexTypes,Tpc_Sprocs) — pure relocations of constraint/index emission after column mappings.Generated code, before vs after