-
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
Deletion can cause error "The association between entity types A and B has been severed..." #33365
Comments
@edwiles This line, In the cascade case, severing the relationship causes the orphans to be marked as |
@ajcvickers Thanks. I see that replacing that line with root.Outputs.ThingOutputs.RemoveAt(0) fixes the code here. But our "real" code uses AutoMapper (with the Collections extension) to map, for example, a list of 0 ThingOutputs elements on to a list of 1 element. The deletion there results in this error, and I believe it is using Remove (see this file line 40). . I'll try to change this toy example to more accurately reflect the real code - please can we keep the issue open for the time being - thanks. |
@ajcvickers Actually, I spoke too soon - replacing that line with I can understand why that line in itself doesn't mark the deleted entity as Deleted, but are we saying |
@edwiles The Removing from a collection navigation docs might be useful here. |
@ajcvickers thanks a lot for this. As a workaround, I'll make the FK optional. Case closed! |
When multiple cascade paths have been avoided by setting one path to
DeleteBehavior.Restrict
, deletion can fail with the error:The code below runs successfully when
DeleteBehavior
isCascade
, but fails whenRestrict
, even though the deleted entity should not be affected by this setting.This is related to the fixed issue #30122, but is not the same.
Steps to reproduce
.csproj
file.Model.cs
:Program.cs
:The program shows that the ThingOutputs entity has been deleted, but then fails with an exception claiming that the association between Outputs and ThingOutputs has been severed.
I believe this error should not appear, because we deleted the child not the parent (?).
Comment out
Model.cs
line 30 (i.e. the call toSetForeignKeyDeleteBehaviour
), so that the delete behaviour remains Cascade.Run the program again:
The same counts are shown, i.e. no cascade deletion was required, and there is no error.
Further technical details
EF Core version: 8.0.3
.NET SDK: 8.0.200
Database Provider: Microsoft.EntityFrameworkCore.Sqlite (though in our "real" code we use SqlServer)
Operating system: Windows 11 Enterprise
IDE: Visual Studio 17.9.2
The text was updated successfully, but these errors were encountered: