Repository navigation
Deferrable foreign keys - #39443
Deferrable foreign keys#39443
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The changes break compatibility with previously compiled migrations and existing provider implementations, while initially-immediate SQL emission lacks successful test coverage.
3 open issues
2 files not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated filesrc/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Generated file
What changed in this PR
Adds design-time support for deferrable foreign keys across EF Core’s relational metadata and migrations pipeline, addressing #11903. SaveChanges dependency ordering remains outside this PR’s scope.
Changes:
- Adds deferrability metadata, fluent configuration, and shared-constraint validation.
- Carries deferrability through migration differencing, snapshots, generated C#, and SQL.
- Adds SQLite coverage and SQL Server rejection paths for unsupported constraints.
| File | Description |
|---|---|
| test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs | Tests rejection of deferrable relationships. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs | Tests unsupported migration-operation errors. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs | Adds migration rejection coverage. |
| test/EFCore.Sqlite.FunctionalTests/Migrations/SqliteMigrationsSqlGeneratorTest.cs | Adds create-table and rebuild SQL baselines. |
| test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs | Tests deferrable foreign-key migrations. |
| test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs | Tests addition, alteration, and unchanged constraints. |
| test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs | Tests mismatched shared-constraint deferrability. |
| test/EFCore.Relational.Tests/Design/AnnotationCodeGeneratorTest.cs | Tests fluent configuration generation. |
| test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs | Adds shared migration coverage. |
| test/EFCore.Relational.Specification.Tests/Migrations/MigrationsSqlGeneratorTestBase.cs | Adds shared SQL-generation scenarios. |
| test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorTest.ModelSnapshot.cs | Tests snapshot persistence. |
| test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationOperationGeneratorTest.cs | Tests migration-code round trips. |
| src/EFCore.SqlServer/Properties/SqlServerStrings.resx | Adds unsupported-deferrability messages. |
| src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs | Exposes generated message accessors. |
| src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs | Rejects deferrable migration operations. |
| src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs | Rejects deferrable model relationships. |
| src/EFCore.Relational/Properties/RelationalStrings.resx | Adds shared-constraint mismatch diagnostics. |
| src/EFCore.Relational/Properties/RelationalStrings.Designer.cs | Exposes the mismatch message accessor. |
| src/EFCore.Relational/Migrations/Operations/Builders/CreateTableBuilder.cs | Accepts deferrability for inline foreign keys. |
| src/EFCore.Relational/Migrations/Operations/AddForeignKeyOperation.cs | Stores and copies deferrability. |
| src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs | Emits SQL-standard deferrability clauses. |
| src/EFCore.Relational/Migrations/MigrationBuilder.cs | Accepts deferrability when adding foreign keys. |
| src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs | Detects deferrability changes. |
| src/EFCore.Relational/Metadata/RelationalAnnotationNames.cs | Defines the deferrability annotation. |
| src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs | Checks shared-constraint compatibility. |
| src/EFCore.Relational/Metadata/Internal/ForeignKeyConstraint.cs | Reads configured deferrability. |
| src/EFCore.Relational/Metadata/IForeignKeyConstraint.cs | Exposes constraint deferrability. |
| src/EFCore.Relational/Metadata/Conventions/RelationalRuntimeModelConvention.cs | Removes deferrability from runtime metadata. |
| src/EFCore.Relational/Extensions/RelationalForeignKeyExtensions.cs | Adds metadata accessors and configuration-source lookup. |
| src/EFCore.Relational/Extensions/RelationalForeignKeyBuilderExtensions.cs | Adds relationship and ownership configuration APIs. |
| src/EFCore.Relational/EFCore.Relational.baseline.json | Records the updated public API surface. |
| src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs | Omits deferrability from compiled models. |
| src/EFCore.Relational/Design/AnnotationCodeGenerator.cs | Generates HasDeferrability calls. |
| src/EFCore.Relational/ConstraintDeferrability.cs | Defines the three constraint modes. |
| src/EFCore.Design/Migrations/Design/MigrationsCodeGenerator.cs | Includes the enum namespace when needed. |
| src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs | Emits deferrability arguments in migration code. |
Review effort: Balanced
8568447 to
656ddf5
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The convention-aware overload lacks focused tests for configuration precedence, rejected changes preserving the value and source, and null resets restoring NotDeferrable and clearing the source.
3 open issues
1 resolved since last review
2 files not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated filesrc/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Generated file
Review effort: Balanced
656ddf5 to
2388300
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Intentional binary/interface breaks in the relational API and required external-provider integration need human API review of release policy and provider coordination.
2 open issues
1 resolved since last review
2 files not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated filesrc/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Generated file
Review effort: Balanced
2388300 to
afd10f9
Compare
Adds ConstraintDeferrability and HasDeferrability() on relationship builders (relational annotation Relational:Deferrability), flows it to IForeignKeyConstraint.Deferrability, AddForeignKeyOperation.Deferrability, MigrationBuilder.AddForeignKey/CreateTableBuilder.ForeignKey, the model differ (drop and re-add on change), the model snapshot and migration code generation. MigrationsSqlGenerator emits the SQL standard DEFERRABLE INITIALLY IMMEDIATE/DEFERRED clause (used by SQLite, including table rebuilds); SQL Server throws NotSupportedException. Part of dotnet#11903
SqlServerModelValidator throws DeferrableForeignKeysNotSupported when a foreign key is configured as deferrable, following SQLite's handling of unsupported features. SqlServerMigrationsSqlGenerator still throws NotSupportedException for migration operations without a model.
afd10f9 to
45d86eb
Compare
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
API review baseline changes for
|



Adds deferrable foreign key constraints metadata to the core API. Closes #11903.
Changes
ConstraintDeferrabilityNotDeferrable,DeferrableInitiallyImmediate,DeferrableInitiallyDeferredRelational:Deferrabilityannotation.Get/SetDeferrability,GetDeferrabilityConfigurationSource.HasDeferrability/CanSetDeferrabilityon relationship and ownership builders (generic and convention variants).IForeignKeyConstraint.Deferrability. A design-time-only annotation, removed from the runtime and compiled model.DuplicateForeignKeyDeferrabilityMismatchfor FKs that share a constraint name. SQL Server:SqlServerModelValidatorreports a deferrable FK as a model validation error (DeferrableForeignKeysNotSupported), following SQLite's handling of unsupported features.AddForeignKeyOperation.Deferrability.deferrability:parameter onMigrationBuilder.AddForeignKeyandCreateTableBuilder.ForeignKey. The differ drops and re-adds an FK when its deferrability changes.MigrationsSqlGenerator.Deferrability(...)emits the SQL-standard clause. SQLite uses it inline and through table rebuilds. For migration operations without a model (hand-written migrations), SQL Server throwsNotSupportedException(DeferrableConstraintsNotSupported).HasDeferrability(...). Migration code generation emitsdeferrability:and addsusing Microsoft.EntityFrameworkCore;.Tests
MigrationsSqlGeneratorTestBase: add FK (both modes), create table with deferrable FK.MigrationsTestBase.Add_deferrable_foreign_key.SqlServerModelValidatorTest: a deferrable FK throws in both modes;NotDeferrableand unset FKs passNotSupportedExceptionassertions for migration operationsAddForeignKeyandCreateTablecode generation round-trip, model snapshotBreaking changes
MigrationBuilder.AddForeignKeyandCreateTableBuilder.ForeignKey. This is a binary break.IForeignKeyConstraint.Deferrability.Provider notes
ForeignKeyConstraint, so it needs to callDeferrability(operation.Deferrability, builder).Deferrabilityto throw.Follow-ups (not in this PR)