-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
DeleteBehavior.Restrict behaves like ClientSetNull #18959
Comments
@Neme12 |
@ajcvickers I would argue that the name is confusing then.
But |
@ajcvickers I think I see your point now. So This design seems really unfortunate because the enum is not a flags enum which means that I cannot set the database behavior Also, I don't see how it would make any sense to set some EF Core fixup behavior (like In any case, the API is really confusing as it is right now. |
@Neme12 This may be helpful: #12661 (comment) |
This change is really confusing, and broke our app while upgrading from 2.1 to 3.1. Having "Restrict" not actually restricting, but like ClientSetNull is misleading. I'm not against having an option for that behaviour, but shipping it with the name "Restrict" which used to restrict deletions in earlier versions is just a hidden trap, close to perfidiousness! |
DeleteBehavior.Restrict
seems to behave exactly likeClientSetNull
. Looking at the docs https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-3.0, I can't see any difference betweenRestrict
andClientSetNull
. Is there any?This was very unexpected to me because I expected
Restrict
to just show a database error if I delete the referenced entity as opposed to EF Core silently setting the foreign key to null. Instead, I spent a lot of time debugging and wondering how the column could have become null. IfRestrict
behaves likeClientSetNull
, is there any option that actually behaves like restrict and opt-out of EF Core interfering with my foreign keys and silently setting them to null?My scenario for this is:
I have a parent entity and child entities with cascading delete behavior. One of the children is special and is needed almost every time the parent entity is needed, and to simplify a lot of queries, we introduced a foreign key that points from the parent entity to this single child entity. It has to be nullable because it's not possible to create a cyclic dependency with one query (because the entities don't have IDs yet). The
DeleteBehavior
should beRestrict
because deleting this child entity should never delete the parent - deleting the child entity while there's a reference to it from the parent is always a bug in the code (the key needs to be changed to point to a different child entity - it shouldn't ever be null except for the purpose of creating the cyclic dependency). IfRestrict
didn't have the set null behavior, I could have caught this bug early.The text was updated successfully, but these errors were encountered: