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

Restrict behaves like ClientSetNull in EF Core 2.2 Npgsql #16588

Closed
ArthDevRepo opened this issue Jul 12, 2019 · 9 comments
Closed

Restrict behaves like ClientSetNull in EF Core 2.2 Npgsql #16588

ArthDevRepo opened this issue Jul 12, 2019 · 9 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ArthDevRepo
Copy link

I'm using EF Core 2.2 and when i set Delete Behaivor in a relationship to Restrict, the foreign key is set to null in tracked entities when they are made orphans. This is not in accordance with the behavior described in this article: do not set foreign key property in the child entity to null.

Article: https://docs.microsoft.com/pt-br/ef/core/saving/cascade-delete#feedback

Source code in:
https://github.com/ArthDevRepo/EfCoreRestrictBehaivorProblem

Expected behaivor: When Restrict, do not set FK Property to NULL
Actual behaivor: When Restrict, set FK Property to NULL.

@ajcvickers
Copy link
Member

@ArthDevRepo The issue here is that the documentation is incorrect. Restrict (in 2.2--see #12661 for 3.0 changes) means EF will not attempt to set the FK to null or delete a dependent entity when a principal entity is deleted. However, setting the FK explicitly to null either directly or through a corresponding change to the navigation property (as in your example) is still honored. So in a case like in your example, where the FK is nullable and the relationship is optional, setting the FK to null is fine, and can be persisted to the database without error. No orphans are deleted and there is no FK violation.

@ArthDevRepo
Copy link
Author

I want to change de navigation property without setting fk to null, any chance to do it?

@ArthDevRepo
Copy link
Author

From https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-2.1

Indicates how a delete operation is applied to dependent entities in a relationship when the principal is deleted or the relationship is severed.

Restrict

For entities being tracked by the DbContext, the values of foreign key properties in dependent entities are not changed. This can result in an inconsistent graph of entities where the values of foreign key properties do not match the relationships in the graph. If a property remains in this state when SaveChanges() is called, then an exception will be thrown.

@ajcvickers
Copy link
Member

@ArthDevRepo Can you explain why you want to do that?

@ArthDevRepo
Copy link
Author

ArthDevRepo commented Jul 16, 2019

Remove the post from blog posts list. Or set post.blog navigation property to NULL.
Expected result is the FK property not changed, if using Restrict delete behaivor , seeing Enum documentation above.
Thanks!

@ajcvickers
Copy link
Member

@ArthDevRepo But why do you want to do that? (As I said, the documentation here is wrong.)

@ArthDevRepo
Copy link
Author

I know that doing this does not make sense since I'm deleting the relationship explicitly. My question was whether I was doing it the right way, since the documentation said that FK was not changed. Testing again, I noticed that when deleting the relationship (remove the post from blogs post list), configured with DeleteBehaivor.Cascade, the FK is not changed as with ClientSetNull, SetNull and Restrict. Is that correct? thank you!

@ajcvickers
Copy link
Member

@ArthDevRepo Whether or not an FK is set to null depends on whether or not the relationship is required or not, in addition to the delete behavior. A required relationship implies the FK is non-nullable, and hence cannot be set to null.

@ArthDevRepo
Copy link
Author

When a relationship is severed, the fk is set to null if cascade behaivor is clientsetnull, setnull and restrict. Cascade never touch the fk

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jul 19, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants