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

Nullable foreign key property deletes self entity from the database when set to null #27260

Closed
VincentVanclef opened this issue Jan 24, 2022 · 1 comment

Comments

@VincentVanclef
Copy link

VincentVanclef commented Jan 24, 2022

Hi there,

So recently we had issues with a client reporting data being deleted, and we asked him for a step-by-step on what he did before the data went missing. We were a bit confused, as the step did not call for any direct Delete methods. So we went and checked our table configurations for any bugged Cascading Deletes.

However, the cascading delete looks to be correct both code-wise and in the database (We always use the build in RelationalDatabaseCreator to ensure the DB and Code is in sync). The class has a nullable property, which was a foreign key, when that property was set to NULL, it deleted the class entity from the database? This makes no sense to me.

Im not so sure whether this is a bug, or intended, in either case I would love to know why this is happening.

  1. Create CAR entity:
    image

  2. Attach two Drivers to the car
    image

So far so good..

Now, i want to remove the CAR from Driver 1 by setting CarId to NULL, this triggers a DELETE command to the database and completely removes the driver, why is this? The foreign key ive set in the DriverConfiguration should (from how i understand it) make it so IF the Car gets deleted, then the Driver is deleted as well.

image

I have attached the demo project here: test.zip

EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer 6.0.0
Target framework: (e.g. .NET 5.0) .net 6
Operating system: windows 10
IDE: visual studio 2022

@ajcvickers
Copy link
Contributor

Duplicate of #27174.

@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
Projects
None yet
Development

No branches or pull requests

2 participants