-
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
Entity deleted when required principal is swapped instead of an exception. #22391
Comments
Since "fk_PersonId" is unique one of the cars needs to have the Owner set to null temporarily and saved, then the correct owners can be set. This will be done automatically when #1699 is implemented When you call |
I investigated this and tracked it down to an automatic delete-orphans behavior that happens while working through the re-parenting. The workaround is to use:
|
We have two entities, cars and persons. Every car belongs to 0 or 1 person and every person owns 0 or 1 car.
So it is a 0..1 to 0..1 relationship between the two entities.
Let's load two cars that have each an owner:
Then swap the owners of the two cars:
and save it back to the database with
context.SaveChanges();
. This throws the following exception:NOTE: Everything works fine if the relationship is not required (
isRequired(false)
).Steps to reproduce
Here is the example code that reproduces the issue:
Further technical details
EF Core version: 3.1.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .Net 4.7.2
Operating system: Windows 10
IDE: Visual Studio 2019 16.4.4
The text was updated successfully, but these errors were encountered: