Skip to content

Validate that ownership relationships keep their default DeleteBehavior and requiredness#38448

Open
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-validation-deletebehavior
Open

Validate that ownership relationships keep their default DeleteBehavior and requiredness#38448
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-validation-deletebehavior

Conversation

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Bulk mutations like foreach (var fk in model.GetForeignKeys()) fk.DeleteBehavior = DeleteBehavior.Restrict; silently overwrite the Cascade behavior that ownership FKs require. The runtime keeps treating the relationship as a cascading required ownership while the migrations snapshot reflects the user-set value, producing endless drop/recreate FK migrations (#33625 and many duplicates).

Per @AndriySvyryd, surface this as a model validation error instead of letting the model be silently inconsistent.

Changes

  • ModelValidator.ValidateOwnership — throw when the ownership FK has DeleteBehavior != Cascade or IsRequired == false.
  • CoreStrings — two new messages:
    • OwnershipNotCascadeDelete(principal, dependent, deleteBehavior, cascadeBehavior)
    • OwnershipNotRequired(principal, dependent)
  • ModelValidatorTestDetects_ownership_with_non_cascade_delete_behavior and Detects_optional_ownership.

Example

modelBuilder.Entity<Blog>().OwnsOne(b => b.Metadata);

// Now throws on model finalization instead of silently corrupting migrations:
foreach (var fk in modelBuilder.Model.GetEntityTypes().SelectMany(t => t.GetForeignKeys()))
{
    fk.DeleteBehavior = DeleteBehavior.Restrict;
}

The ownership relationship from 'Blog' to 'Blog.Metadata#Metadata' is configured with 'Restrict' delete behavior. Ownership relationships must use 'Cascade' delete behavior. Either remove the explicit delete behavior configuration or don't configure this relationship as an ownership.

Breaking change note

Models that previously built successfully while overriding DeleteBehavior/IsRequired on ownership FKs will now fail validation. The runtime behavior was already ignoring these overrides, so the only fix on the user side is to skip ownership FKs when applying the override.

…an ownership

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Throw in validation for changed DeleteBehavior or requiredness Validate that ownership relationships keep their default DeleteBehavior and requiredness Jun 17, 2026
Copilot AI requested a review from AndriySvyryd June 17, 2026 23:06
@AndriySvyryd AndriySvyryd requested a review from Copilot June 17, 2026 23:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request strengthens EF Core’s model validation to prevent ownership relationships from becoming internally inconsistent when users bulk-mutate foreign keys (e.g., setting DeleteBehavior globally). It ensures that ownership FKs cannot be configured away from the semantics EF Core requires, avoiding endless “drop/recreate FK” migrations caused by mismatched snapshot vs. runtime expectations.

Changes:

  • Add model validation errors for ownership foreign keys configured with non-Cascade DeleteBehavior.
  • Add model validation errors for ownership foreign keys configured as optional (IsRequired == false).
  • Add corresponding localized error strings and unit tests covering both invalid configurations.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs Adds regression tests for ownership FK DeleteBehavior != Cascade and IsRequired == false.
src/EFCore/Properties/CoreStrings.resx Adds two new user-facing validation messages for invalid ownership FK configuration.
src/EFCore/Properties/CoreStrings.Designer.cs Adds strongly-typed accessors for the new CoreStrings messages.
src/EFCore/Infrastructure/ModelValidator.cs Implements the new ownership validation checks in ValidateOwnership.
Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

Comment thread src/EFCore/Properties/CoreStrings.Designer.cs
Comment thread src/EFCore/Infrastructure/ModelValidator.cs
… baseline

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Comment thread src/EFCore/Infrastructure/ModelValidator.cs
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…ions

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
@AndriySvyryd AndriySvyryd force-pushed the copilot/fix-validation-deletebehavior branch from 4d53ef4 to 98e7ffa Compare June 18, 2026 07:19
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 18, 2026 07:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 8 changed files in this pull request and generated no new comments.

Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Generated file

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.

3 participants