-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Non-nullable FK's get On Delete Behavior of ClientSetNull when scaffolding #9935
Comments
@jackjwilliams Required relationships default to The documentation describes the behavior of the EF Core runtime, but it seems in this case .OnDelete(DeleteBehavior.ClientSetNull) I suspect we may be doing this on purpose because there is no ON DELETE CASCADE specified in the database, although I would like us to take a closer look (perhaps |
Notes from triage: ClientSetNull for a required relationship (non-nullable FK) means that if an attempt is made to set the FK to null, then an exception will be thrown on SaveChanges. This is functionally correct for this case, but after the change to Restrict in #9703 it might be clearer to start reverse engineering to Restrict at that time. Closing this as a dupe of #9703 with a note on #9703 to update reverse engineering. |
Thanks for the triage and explanation of what exactly this does. The documentation on the enum was confusing and I'm not sure if it ever just came out and said "ClientSetNull for a required relationship (non-nullable FK) means that if an attempt is made to set the FK to null, then an exception will be thrown on SaveChanges" ... Which I think relates to #9687. |
Maybe I'm misunderstanding what ClientSetNull does, but when using dotnet ef dbcontext scaffold, if I have a required relationship from a child to a parent (non-nullable foreign key), scaffold always generates the modelBuilder with ClientSetNull behavior on delete.
I'm not sure why, the field in the database is not null, the property generated is not nullable.
The documentation (when hovering) states:
"This is the default for optional relationships. That is, relationships that have nullable foreign keys".
Expected behavior:
DeleteBehavior.Cascade is used instead. If a child has a non-nullable FK that indicates it cannot live without the parent. If the parent dies, so must the child.
Or maybe I'm misunderstanding something and need to quit coding after 2AM :>.
Steps to reproduce
Create Database
Scaffold
dotnet ef dbcontext scaffold "Data Source=.\SQLExpress;Integrated Security=True;Database=Test;MultipleActiveResultSets=True" Microsoft.EntityFrameworkCore.SqlServer -c TestContext -o Models
Output
Project Link
https://1drv.ms/u/s!Aig3X163M_PDg7U24JBJH1gamRQpCw
Further technical details
EF Core version: 2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.3.5
The text was updated successfully, but these errors were encountered: