-
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
Preserve DeleteBehavior when scaffolding a database created by EF Core #21252
Comments
@JonPSmith |
Yes, I can see your reasoning and it makes sense. I thought a non-nullable foreign key would be enough to decide on a better DeleteBehavior, but you don't know if its a Thanks for taking the time to explain that. I have marked this as closed. |
Sorry @ajcvickers , but this one was bugging me. I think if the the foreign key is non-nullable you should set the DeleteBehavior to You have other, more important things to do so don't do anything about this, but I had to say that because it was going around in my head. |
@JonPSmith I believe that the behavior of Restrict and ClientSetNull will be the same for non-cascading required relationships. I'm going to re-open this to discuss whether we should use Restrict instead since it will be functionally the same. |
Hi @ErikEJ, I wondered if you give your feedback on this issue I raised scaffolding. Super-quick summary: If the SQL Delete constraint is Do you have a view on the right state, and more importantly if this was changed would it cause a problem to anyone already using scaffolding? As you and your company use scaffolding in production I expect you will have a view on this :) |
@JonPSmith Given that the foreign keys and their delete behaviour is defined in the database, I fail to see how this makes much difference if any if scaffolding from an existing database. (I never use CASCADE anyway - too much dangerous magic) |
Putting this on the backlog to scaffold the same value that the database schema indicates whenever possible. |
Current flow of values:
Note: SQL Server doesn't support RESTRICT so we use NO ACTION instead There seems to be a lot of room for improvement. 🙂 |
I have built a system around checking dependencies before deleting an entity so that: With reverse engineering I would expect the modelled delete behaviour to match the database. If I desire some client behaviour such as ClientCascade, I can modify as such and the delete routine will load and audit them for deletion. In the given example (unfortunately one of many) the user will delete an Applicant, be given no warning about dependent ApplicationForms, then EF will throw an exception trying to set the FK to null. Because I have built a system on the assumption the metadata EF holds about the database is accurate, and where it isn’t it’s because I’ve manually modified it. But currently it’s not accurate off the bat. |
Can I ask for clarity over delete behaviour in EF Core 3.1 please. This documentation for But the enum documentation for Can I correctly assume that the first documentation is correct and the enum one wrong? As a side note, NoAction also has the same enum comment as Restrict, but doesn't feature in the first documentation. I'm making an assumption that they have the same behaviour client side and both exist just to match different database options among different providers. But it'd be nice not to have to make assumptions about any of this. |
@Snappyfoo The cascade delete docs are out-of-date. This issue is tracking updating the docs: dotnet/EntityFramework.Docs#473 I believe the enum documentation is correct, since it was updated when the behavior was updated before 3.0. Note that each value describes both client and database behaviors. This means that there isn't a 1:1 mapping between the database value and the EF value. However, there should not be a case where the database has a different behavior from that which is reverse engineered. |
Here are my proposed changes: Scaffolding
Migraitons
|
I have a test database where I set the
DeleteBehaviour
of one line toRestrict
. When I reverse engineer it theDeleteBehaviour
is set toClientSetNull
. As the foreign key isn't nullableClientSetNull
seems an odd result.Steps to reproduce
My DbContext looks like this
The part of the reverse engineered that configures the
LineItem
isI did check the SQL for the LineItem below
Further technical details
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 5-preview5
Operating system: Windows
IDE: Visual Studio 2019 16.6
The text was updated successfully, but these errors were encountered: