Skip to content

Research delete behaviors with PostgreSQL and EF Core #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

bkoelman
Copy link
Member

@bkoelman bkoelman commented Oct 25, 2022

Added model and tests that represent the scenario described at https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete#database-cascade-limitations

The article states various limitations in SQL Server:

Some databases, most notably SQL Server, have limitations on the cascade behaviors that form cycles.

Using cascading deletes and cascading nulls in the database at the same time will almost always result in relationship cycles when using SQL Server.

When all related entities are loaded in the EF Core change tracker upfront, EF Core is able to handle the scenario by issuing multiple SQL statements. And that's why the default delete behavior for optional relationships is ClientSetNull instead of SetNull.

The tests in this commit show that PostgreSQL handles the presented scenario successfully. Letting the database handle cascading is a lot more efficient.

NOTE: This PR was created to demonstrate that the default ClientSetNull isn't the best choice for PostgreSQL. Not intended for merging.

…//learn.microsoft.com/en-us/ef/core/saving/cascade-delete#database-cascade-limitations

The article states various limitations in SQL Server:

> Some databases, most notably SQL Server, have limitations on the cascade behaviors that form cycles.

> Using cascading deletes and cascading nulls in the database at the same time will almost always result in relationship cycles when using SQL Server.

When all related entities are loaded in the EF Core change tracker upfront, EF Core is able to handle the scenario by issuing multiple SQL statements.
And that's why the default delete behavior for optional relationships is `ClientSetNull` instead of `SetNull`.

The tests in this commit show that PostgreSQL handles the presented scenario successfully. Letting the database handle cascading is a lot more efficient.
@codecov
Copy link

codecov bot commented Oct 26, 2022

Codecov Report

Merging #1205 (c008d05) into master (af46168) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1205   +/-   ##
=======================================
  Coverage   92.69%   92.69%           
=======================================
  Files         243      243           
  Lines        7803     7803           
=======================================
  Hits         7233     7233           
  Misses        570      570           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@bkoelman bkoelman closed this Oct 26, 2022
@bkoelman bkoelman deleted the delete-behaviors branch October 26, 2022 01:07
@bkoelman
Copy link
Member Author

Picture of the used model below.

PXL_20221026_090843118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant