Skip to content

Add support for ON DELETE SET DEFAULT - #37380

Merged
AndriySvyryd merged 7 commits into
dotnet:mainfrom
thromel:feature/delete-behavior-set-default
Dec 22, 2025
Merged

AndriySvyryd merged 7 commits into
dotnet:mainfrom
thromel:feature/delete-behavior-set-default

Conversation

@thromel

@thromel thromel commented Dec 15, 2025

Copy link
Copy Markdown
Contributor

Summary

Adds SetDefault and ClientSetDefault values to the DeleteBehavior enum, enabling EF Core to support the SQL standard ON DELETE SET DEFAULT referential 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

  • DeleteBehavior.cs: Add SetDefault (database-enforced) and ClientSetDefault (client-side only) enum values with XML documentation
  • RelationalModel.cs: Update ToReferentialAction() mapping - SetDefault maps to ReferentialAction.SetDefault, ClientSetDefault maps to NoAction
  • StateManager.cs: Update CascadeDelete() to set FK properties to their sentinel/default values for SetDefault behaviors
  • InternalEntityEntry.cs: Update HandleConceptualNulls() to handle SetDefault by setting properties to sentinel values
  • RelationalScaffoldingModelFactory.cs: Add scaffolding support for reverse engineering databases with SET DEFAULT constraints

Usage

// Database-enforced SET DEFAULT
modelBuilder.Entity<Post>()
    .HasOne(p => p.Blog)
    .WithMany(b => b.Posts)
    .OnDelete(DeleteBehavior.SetDefault);

// Client-side only (for databases that don't support SET DEFAULT)
modelBuilder.Entity<Post>()
    .HasOne(p => p.Blog)
    .WithMany(b => b.Posts)
    .OnDelete(DeleteBehavior.ClientSetDefault);

// Or via attribute
public class Post
{
    [DeleteBehavior(DeleteBehavior.SetDefault)]
    public Blog Blog { get; set; }
}

Test plan

  • Added unit tests for DeleteBehaviorAttributeConvention with SetDefault and ClientSetDefault
  • Added unit tests for ToReferentialAction mapping with all DeleteBehavior values
  • Verified all existing DeleteBehavior and CascadeDelete tests pass
  • Verified scaffolding tests pass

Fixes #37312

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
@thromel
thromel requested a review from a team as a code owner December 15, 2025 20:00
@thromel

thromel commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

@thromel please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

@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.

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"
@thromel

thromel commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

Thank you for the review! I've addressed the feedback:

  1. Refactored the condition in InternalEntityEntry.HandleConceptualNulls to prioritize the isSetDefault check
  2. Updated the ClientSetDefault documentation to use "sentinel values" instead of "default values"
  3. Added functional tests in GraphUpdatesTestBase for both ClientSetDefault with custom Sentinel value and SetDefault with custom Sentinel value

- 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
@thromel

thromel commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

I've addressed the additional feedback:

  1. Renamed ChildWithSetDefault to ChildWithClientSetDefault for clarity
  2. Added SaveChanges and re-query in a separate lambda to verify the FK value is persisted
  3. Moved the SetDefault test to SQL Server tests (GraphUpdatesSqlServerTestBase) with HasDefaultValue(667) to properly test database-enforced behavior

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.
@thromel
thromel force-pushed the feature/delete-behavior-set-default branch from 88bad8f to dcf4812 Compare December 19, 2025 06:37
@thromel

thromel commented Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

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:

  • Removed HasData seeding for the fallback parent
  • Explicitly create the fallback parent in the first lambda of each test
  • This approach is more reliable across different database providers and test isolation scenarios

- 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
@thromel
thromel force-pushed the feature/delete-behavior-set-default branch from de4a55b to 94cea21 Compare December 19, 2025 08:17
…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)
@thromel
thromel force-pushed the feature/delete-behavior-set-default branch from 3575b00 to 522cb8b Compare December 20, 2025 05:29
@thromel

thromel commented Dec 20, 2025

Copy link
Copy Markdown
Contributor Author

Addressed the feedback - reverted to using no-op overrides for the SetDefault tests in the incompatible fixtures instead of modifying the fixture configurations.

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) December 22, 2025 19:36
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 0b1afb4 into dotnet:main Dec 22, 2025
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.

Support ON DELETE SET DEFAULT

2 participants