Skip to content

Optional entity splitting fragments - #38749

Merged
AndriySvyryd merged 14 commits into
dotnet:mainfrom
anasik:optional-entity-splitting-fragments
Sep 11, 2026
Merged

AndriySvyryd merged 14 commits into
dotnet:mainfrom
anasik:optional-entity-splitting-fragments

Conversation

@anasik

@anasik anasik commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@anasik
anasik requested a review from a team as a code owner August 5, 2026 08:19
@AndriySvyryd
AndriySvyryd requested a review from Copilot August 5, 2026 18:56
@AndriySvyryd AndriySvyryd self-assigned this Aug 5, 2026
@AndriySvyryd AndriySvyryd added this to the 12.0.0 milestone Aug 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adds support for optional entity-splitting mapping fragments in EF Core Relational: a split table row can be considered optional (absent unless at least one non-key value exists), affecting query join shape, validation, update pipeline behavior, runtime model metadata, scaffolding/snapshot generation, and migrations diagnostics.

Changes:

  • Introduces fragment optionality metadata (IsOptional / IsSplitFragmentOptional) and flows it through runtime model building and code generation.
  • Updates relational query translation to use LEFT JOIN and nullable projections for optional fragments.
  • Adjusts update batching and migrations/model validation to account for optional fragments, with added tests and diagnostics (new warning + event payload).

Reviewed changes

Copilot reviewed 40 out of 41 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/EFCore.SqlServer.FunctionalTests/Update/SqlServerUpdateSqlGeneratorTest.cs Adds new usings (currently appear unused).
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/OwnedTypeEntityType.cs Updates scaffold baseline for new runtime fragment ctor parameter.
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedTypeEntityType.cs Updates scaffold baseline for new runtime fragment ctor parameter.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/OwnedTypeEntityType.cs Updates scaffold baseline for new runtime fragment ctor parameter.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel/OwnedTypeEntityType.cs Updates scaffold baseline for new runtime fragment ctor parameter.
test/EFCore.Sqlite.FunctionalTests/Query/OptionalEntitySplittingQuerySqliteTest.cs Adds query coverage for missing vs optional fragment rows and join shape.
test/EFCore.Sqlite.FunctionalTests/OptionalEntitySplittingSqliteTest.cs Adds SaveChanges coverage for optional fragment insert/update/delete scenarios.
test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTestBase.cs Updates MigrationsModelDiffer construction for new logger dependency.
test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs Adds test for optionality-change warning during migration diffing.
test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs Adds validation tests for optional fragments (nullable enforcement, mixed fragments).
test/EFCore.Relational.Specification.Tests/ModelBuilding/RelationalModelBuilderTest.cs Adds model-building tests and test builders for IsOptional() on split tables.
test/EFCore.Design.Tests/Migrations/Design/MigrationScaffolderTest.cs Updates MigrationsModelDiffer construction for new logger dependency.
src/EFCore.Relational/Update/ModificationCommand.cs Adds optional-fragment helpers and changes Modified→Added decision logic.
src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs Skips optional-fragment insert/delete commands when fragment row is effectively absent.
src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect.cs Uses LEFT JOIN and nullable projections for optional fragments.
src/EFCore.Relational/Properties/RelationalStrings.resx Adds new validation error strings and a migrations warning message.
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs Adds strongly-typed accessors and logging definition wiring for new resources.
src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs Injects migrations logger and emits optionality-change warning.
src/EFCore.Relational/Metadata/RuntimeEntityTypeMappingFragment.cs Adds optionality to runtime mapping fragments and exposes it.
src/EFCore.Relational/Metadata/RelationalAnnotationNames.cs Adds annotation name for fragment optionality.
src/EFCore.Relational/Metadata/ITableMappingBase.cs Adds IsSplitFragmentOptional to table mapping metadata (debug string support).
src/EFCore.Relational/Metadata/IReadOnlyEntityTypeMappingFragment.cs Adds IsOptional to fragment metadata (debug string support).
src/EFCore.Relational/Metadata/Internal/TableMappingBase.cs Implements IsSplitFragmentOptional on internal mapping base.
src/EFCore.Relational/Metadata/Internal/RelationalModel.cs Propagates fragment optionality into relational table/view mappings.
src/EFCore.Relational/Metadata/Internal/InternalEntityTypeMappingFragmentBuilder.cs Adds internal builder APIs to set/check fragment optionality.
src/EFCore.Relational/Metadata/Internal/EntityTypeMappingFragment.cs Stores optionality + configuration source and merges it during model building.
src/EFCore.Relational/Metadata/IMutableEntityTypeMappingFragment.cs Adds mutable IsOptional API surface.
src/EFCore.Relational/Metadata/IConventionEntityTypeMappingFragment.cs Adds convention API surface for fragment optionality.
src/EFCore.Relational/Metadata/Conventions/RelationalRuntimeModelConvention.cs Emits runtime fragments with optionality propagated.
src/EFCore.Relational/Metadata/Builders/SplitTableBuilder`.cs Adds public generic IsOptional() builder API.
src/EFCore.Relational/Metadata/Builders/SplitTableBuilder.cs Adds non-generic IsOptional() builder API.
src/EFCore.Relational/Metadata/Builders/OwnedNavigationSplitTableBuilder``.cs Adds public generic IsOptional() builder API for owned navigations.
src/EFCore.Relational/Metadata/Builders/OwnedNavigationSplitTableBuilder.cs Adds non-generic IsOptional() builder API for owned navigations.
src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs Validates optional fragment constraints (nullable properties, no shared-table splitting).
src/EFCore.Relational/EFCore.Relational.baseline.json Updates API baseline for new public APIs and signature changes.
src/EFCore.Relational/Diagnostics/RelationalLoggingDefinitions.cs Adds logging definition slot for new migrations warning.
src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs Adds logger extension emitting warning + DiagnosticSource payload.
src/EFCore.Relational/Diagnostics/RelationalEventId.cs Adds new migrations event id for optionality-change warning.
src/EFCore.Relational/Diagnostics/EntityTypeMappingFragmentEventData.cs Adds new DiagnosticSource payload type for fragment-related events.
src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs Emits IsSplitFragmentOptional and runtime fragment optionality into generated runtime model.
src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs Emits t.IsOptional() in snapshots for optional split fragments.
Files not reviewed (1)
  • src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd AndriySvyryd changed the title Closes #27974: Optional entity splitting fragments Optional entity splitting fragments Sep 11, 2026
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) September 11, 2026 00:57
@AndriySvyryd
AndriySvyryd merged commit 3d6f8b9 into dotnet:main Sep 11, 2026
43 of 47 checks passed
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Sep 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore.Relational/EFCore.Relational.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

+ class Microsoft.EntityFrameworkCore.Diagnostics.EntityTypeMappingFragmentEventData : Microsoft.EntityFrameworkCore.Diagnostics.EventData
+ virtual IEntityType EntityType { get; }
+ virtual bool IsOptional { get; }
+ virtual StoreObjectIdentifier StoreObject { get; }
+ EntityTypeMappingFragmentEventData(EventDefinitionBase eventDefinition, Func<EventDefinitionBase, EventData, string> messageGenerator, IEntityType entityType,
+     StoreObjectIdentifier storeObject, bool optional);
  static class Microsoft.EntityFrameworkCore.Diagnostics.RelationalEventId
+ static readonly Microsoft.Extensions.Logging.EventId EntitySplittingFragmentOptionalityChangedWarning
  static class Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions
+ static void EntitySplittingFragmentOptionalityChangedWarning(this IDiagnosticsLogger<Migrations> diagnostics, IEntityType entityType,
+     StoreObjectIdentifier storeObject, bool optional);
  static class Microsoft.EntityFrameworkCore.Diagnostics.RelationalStrings
+ static string EntitySplittingNonNullablePropertyOnOptionalFragment(object? entityType, object? storeObject, object? property);
+ static string EntitySplittingOptionalFragmentSharedTable(object? entityType, object? storeObject, object? principalEntityType);
+ static string ExecuteUpdateOnOptionalEntitySplittingFragment(object? entityType, object? property, object? storeObject);
  class Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationSplitTableBuilder :
      Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder>
+ virtual OwnedNavigationSplitTableBuilder IsOptional(bool optional = true);
  class Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationSplitTableBuilder<TOwnerEntity,
      TDependentEntity> : Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationSplitTableBuilder,
      Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder<TOwnerEntity,
      TDependentEntity>> where TOwnerEntity : class where TDependentEntity : class
+ virtual OwnedNavigationSplitTableBuilder<TOwnerEntity, TDependentEntity> IsOptional(bool optional = true);
  class Microsoft.EntityFrameworkCore.Metadata.Builders.SplitTableBuilder :
      Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder>
+ virtual SplitTableBuilder IsOptional(bool optional = true);
  class Microsoft.EntityFrameworkCore.Metadata.Builders.SplitTableBuilder<TEntity> : Microsoft.EntityFrameworkCore.Metadata.Builders.SplitTableBuilder,
      Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<TEntity>> where TEntity :
      class
+ virtual SplitTableBuilder<TEntity> IsOptional(bool optional = true);
  interface Microsoft.EntityFrameworkCore.Metadata.IConventionEntityTypeMappingFragment :
      Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityTypeMappingFragment, Microsoft.EntityFrameworkCore.Infrastructure.IReadOnlyAnnotatable,
      Microsoft.EntityFrameworkCore.Metadata.IConventionAnnotatable
+ ConfigurationSource? GetIsOptionalConfigurationSource();
+ bool? SetIsOptional(bool? optional, bool fromDataAnnotation = false);
  interface Microsoft.EntityFrameworkCore.Metadata.IMutableEntityTypeMappingFragment :
      Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityTypeMappingFragment, Microsoft.EntityFrameworkCore.Infrastructure.IReadOnlyAnnotatable,
      Microsoft.EntityFrameworkCore.Metadata.IMutableAnnotatable
+ bool IsOptional { get; set; }
  interface Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityTypeMappingFragment : Microsoft.EntityFrameworkCore.Infrastructure.IReadOnlyAnnotatable
+ bool IsOptional { get; }
  interface Microsoft.EntityFrameworkCore.Metadata.ITableMappingBase : Microsoft.EntityFrameworkCore.Infrastructure.IAnnotatable,
      Microsoft.EntityFrameworkCore.Infrastructure.IReadOnlyAnnotatable
+ bool IsSplitFragmentOptional { get; }
  static class Microsoft.EntityFrameworkCore.Metadata.RelationalAnnotationNames
+ const string EntityTypeMappingFragmentIsOptional
  class Microsoft.EntityFrameworkCore.Metadata.RuntimeEntityTypeMappingFragment : Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase,
      Microsoft.EntityFrameworkCore.Metadata.IEntityTypeMappingFragment, Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityTypeMappingFragment,
      Microsoft.EntityFrameworkCore.Infrastructure.IReadOnlyAnnotatable, Microsoft.EntityFrameworkCore.Infrastructure.IAnnotatable
- RuntimeEntityTypeMappingFragment(RuntimeEntityType entityType, in StoreObjectIdentifier storeObject, bool? isTableExcludedFromMigrations);
+ RuntimeEntityTypeMappingFragment(RuntimeEntityType entityType, in StoreObjectIdentifier storeObject, bool? isTableExcludedFromMigrations,
+     bool isOptional = false);
+ virtual bool IsOptional { get; }
  class Microsoft.EntityFrameworkCore.Update.ModificationCommand : Microsoft.EntityFrameworkCore.Update.IModificationCommand,
      Microsoft.EntityFrameworkCore.Update.IReadOnlyModificationCommand, Microsoft.EntityFrameworkCore.Update.INonTrackedModificationCommand
+ virtual bool IsOptionalSplitFragment { get; }
+ virtual bool IsOptionalSplitFragmentPayloadAllNull { get; }
+ virtual bool IsOptionalSplitFragmentRowAssumedAbsent { get; }

@anasik
anasik deleted the optional-entity-splitting-fragments branch September 21, 2026 09:39
AndriySvyryd pushed a commit that referenced this pull request Sep 25, 2026
@AndriySvyryd AndriySvyryd removed the api-review This PR or issue is introducing public API changes that need to be reviewed label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants