Validate that ownership relationships keep their default DeleteBehavior and requiredness#38448
Open
Copilot wants to merge 5 commits into
Open
Validate that ownership relationships keep their default DeleteBehavior and requiredness#38448Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
…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
There was a problem hiding this comment.
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-
CascadeDeleteBehavior. - 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
… baseline Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…ions Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
4d53ef4 to
98e7ffa
Compare
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.
Bulk mutations like
foreach (var fk in model.GetForeignKeys()) fk.DeleteBehavior = DeleteBehavior.Restrict;silently overwrite theCascadebehavior 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 hasDeleteBehavior != CascadeorIsRequired == false.CoreStrings— two new messages:OwnershipNotCascadeDelete(principal, dependent, deleteBehavior, cascadeBehavior)OwnershipNotRequired(principal, dependent)ModelValidatorTest—Detects_ownership_with_non_cascade_delete_behaviorandDetects_optional_ownership.Example
Breaking change note
Models that previously built successfully while overriding
DeleteBehavior/IsRequiredon 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.