Repository navigation
Add support for ON DELETE SET DEFAULT - #37380
Conversation
Adds `SetDefault` and `ClientSetDefault` values to the `DeleteBehavior` enum, enabling EF Core to support the SQL standard `ON DELETE SET DEFAULT` referential action. Changes: - Add SetDefault and ClientSetDefault to DeleteBehavior enum - Update ToReferentialAction() mapping in RelationalModel - Update CascadeDelete() in StateManager to set FK to sentinel values - Update HandleConceptualNulls() in InternalEntityEntry - Add scaffolding support for reverse engineering - Add unit tests for attribute convention and ToReferentialAction mapping Fixes dotnet#37312
@dotnet-policy-service agree |
AndriySvyryd
left a comment
There was a problem hiding this comment.
Also add functional tests, could be in GraphUpdatesTestBase or a different class.
In particular, there should be a test for ClientSetDefault with a custom Sentinel value and a test for SetDefault with a custom DefaultValue
- Add ClientSetDefault_with_sentinel_value_sets_FK_to_sentinel_on_delete test - Add SetDefault_with_sentinel_value_sets_FK_to_sentinel_on_delete test - Add ParentWithSetDefault, ChildWithSetDefault entity classes - Add ParentWithSetDefaultValue, ChildWithSetDefaultValue entity classes - Configure entities with HasSentinel(667) and appropriate OnDelete behaviors
- Refactor condition in InternalEntityEntry.HandleConceptualNulls to prioritize SetDefault check - Update ClientSetDefault documentation to use "sentinel values" instead of "default values"
|
Thank you for the review! I've addressed the feedback:
|
- Rename ChildWithSetDefault to ChildWithClientSetDefault for clarity - Add SaveChanges and re-query step to ClientSetDefault test to verify persistence - Move SetDefault test to SQL Server tests with HasDefaultValue for proper database-enforced behavior testing
|
I've addressed the additional feedback:
|
Seed a default parent with Id=667 so that orphaned children have a valid parent to reference when their FK is set to the sentinel/default value. This fixes FK constraint violations in relational databases like SQLite.
88bad8f to
dcf4812
Compare
|
Updated the tests to create the fallback parent (Id=667) explicitly in the test setup instead of using seeded data. This ensures the parent exists within the transaction before the FK constraint is evaluated. Changes:
|
- Create fallback parent explicitly in test setup instead of using seeded data - Preserve SetDefault/ClientSetDefault behaviors in ClientNoAction test fixture - Add entity configuration for SetDefault tests in OwnedTest fixture
de4a55b to
94cea21
Compare
…ions - Override SetDefault tests with no-op in ClientNoAction fixture (reverted filter approach) - Override SetDefault tests with no-op in Owned fixture (removed entity config)
3575b00 to
522cb8b
Compare
|
Addressed the feedback - reverted to using no-op overrides for the SetDefault tests in the incompatible fixtures instead of modifying the fixture configurations. |
|
Thanks for your contribution! |
Summary
Adds
SetDefaultandClientSetDefaultvalues to theDeleteBehaviorenum, enabling EF Core to support the SQL standardON DELETE SET DEFAULTreferential action.This allows users to configure foreign key relationships so that when a principal entity is deleted, the dependent entity's FK values are set to their default values instead of being cascaded or set to null.
Changes
SetDefault(database-enforced) andClientSetDefault(client-side only) enum values with XML documentationToReferentialAction()mapping -SetDefaultmaps toReferentialAction.SetDefault,ClientSetDefaultmaps toNoActionCascadeDelete()to set FK properties to their sentinel/default values for SetDefault behaviorsHandleConceptualNulls()to handle SetDefault by setting properties to sentinel valuesUsage
Test plan
DeleteBehaviorAttributeConventionwith SetDefault and ClientSetDefaultToReferentialActionmapping with all DeleteBehavior valuesFixes #37312