Skip to content
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

Request: A DeleteBehavior that does absolutely nothing on delete and on SaveChanges #8633

Closed
Wain123 opened this issue May 29, 2017 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@Wain123
Copy link

Wain123 commented May 29, 2017

DeleteBehavior.Restrict behaves the same as DeleteBehavior.SetNull for tracked entities, details are in #8342

My problem is that, if I delete an entity and forget to handle its children, EF will simply set their foreign keys to null and let me save the mistake to the DB. It goes out of its way to make sure I don't notice my mistake.

I would like to have a DeleteBehavior option that simply does nothing. If a navigation property of a child entity points to a deleted parent entity, I want SaveChanges to send a DELETE command for the parent and leave the child alone, so that my DB can give me a foreign key constraint exception. Alternatively, EF could also give me an exception on SaveChanges telling me that something points to a deleted entity.

Whether the FK is nullable or not also shouldn't matter, don't null it even if it's nullable.

@ajcvickers
Copy link
Member

@Wain123 We discussed this at length and came to a couple of conclusions:

  • This API is really misleading. We had decided to better document it (see OnDelete restrict doesn't allow me delete an entity #8342), but after further discussion we are now going to obsolete it and go back to an API that just specified cascade delete behavior. This will mean there will no longer any confusion over setting SetNull or Restrict and having the DbContext tracking continue to behave in the same way.
  • The behavior in the context is driven by the model metadata. For required relationships, cascade deletes are switched on by default, but if switched off, then EF will throw if an FK needs to be nulled but cannot be. For optional relationships, cascades deletes are off by default (but can be switched on) and the FK is nulled because this is the best semantic representation of what happened in the model. We're not going to support optional relationships where we don't attempt to null FKs. This would leave the model in an inconsistent state, and one of the functions of an OR/M is to help the model and relationships stay in a consistent state.

@ajcvickers ajcvickers added the closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. label May 31, 2017
@ajcvickers ajcvickers reopened this Jun 5, 2017
ajcvickers added a commit that referenced this issue Jun 5, 2017
…and Restrict in the database

Issues #8654, #8632, #8633

The new behavior is called 'ClientSetNull'.
'Restrict' now throws on SaveChanges if EF would have set null. This is the same behavior that has always been there for required relationships, so this is only a break for optional relationships explicitly configured with Restrict, which should be relatively rare since this was the default anyway. The default is now 'ClientSetNull', which matches the old behavior of 'Restrict', so apps that were not setting anything explicitly will not get a breaking change.
@ajcvickers ajcvickers added type-enhancement and removed closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. labels Jun 5, 2017
@ajcvickers ajcvickers self-assigned this Jun 5, 2017
@ajcvickers ajcvickers added this to the 2.0.0-preview2 milestone Jun 5, 2017
@ajcvickers
Copy link
Member

Decided to do this as a side-effect of #8654.

ajcvickers added a commit that referenced this issue Jun 5, 2017
…and Restrict in the database

Issues #8654, #8632, #8633

The new behavior is called 'ClientSetNull'.
'Restrict' now throws on SaveChanges if EF would have set null. This is the same behavior that has always been there for required relationships, so this is only a break for optional relationships explicitly configured with Restrict, which should be relatively rare since this was the default anyway. The default is now 'ClientSetNull', which matches the old behavior of 'Restrict', so apps that were not setting anything explicitly will not get a breaking change.
ajcvickers added a commit that referenced this issue Jun 5, 2017
…and Restrict in the database

Issues #8654, #8632, #8633

The new behavior is called 'ClientSetNull'.
'Restrict' now throws on SaveChanges if EF would have set null. This is the same behavior that has always been there for required relationships, so this is only a break for optional relationships explicitly configured with Restrict, which should be relatively rare since this was the default anyway. The default is now 'ClientSetNull', which matches the old behavior of 'Restrict', so apps that were not setting anything explicitly will not get a breaking change.
ajcvickers added a commit that referenced this issue Jun 6, 2017
…and Restrict in the database

Issues #8654, #8632, #8633

The new behavior is called 'ClientSetNull'.
'Restrict' now throws on SaveChanges if EF would have set null. This is the same behavior that has always been there for required relationships, so this is only a break for optional relationships explicitly configured with Restrict, which should be relatively rare since this was the default anyway. The default is now 'ClientSetNull', which matches the old behavior of 'Restrict', so apps that were not setting anything explicitly will not get a breaking change.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 6, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview2, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

2 participants