Nullable reference with DeleteBehavior.Cascade that is deleted is reloaded back after DetectChanges #26827
Labels
area-change-tracking
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-bug
Milestone
We are using EF with .NET 6.0 and we have an entity which is child of several other entities:
All FKs are nullable and defined on
Car
table and withDeleteBehaviour.Cascade
:We want to be able to delete the child by setting parent reference to null:
After
ChangeTracker.DetectChanges()
is called, the child is correctly marked as deleted but the reference itself is reloaded, soperson.Car != null
. Having a deleted entity hanging in the reference causes trouble for subsequent business logic.I tried to debug the issue, it seems to be a combination of
InternalEntityEntry.SetProperty()
not setting the value of underlying property tonull
because ofDeleteBehaviour.Cascade
and the fact that subsequentChangeDetector.DetectKeyChange()
compares snapshot value (containsnull
) with actual value of underlying property (contains original key).Steps to reproduce
Project:
https://github.com/xsmerek/EFCoreDeletingNullableReference
Steps:
Provider and version information
EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0
Please let me know if you need any additional information.
The text was updated successfully, but these errors were encountered: