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 reference with DeleteBehavior.Cascade that is deleted is reloaded back after DetectChanges #26827

Closed
xsmerek opened this issue Nov 25, 2021 · 1 comment · Fixed by #28395
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

Comments

@xsmerek
Copy link

xsmerek commented Nov 25, 2021

We are using EF with .NET 6.0 and we have an entity which is child of several other entities:

    public partial class Car
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public partial class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Car Car { get; set; } // FK is defined in Car table
    }

All FKs are nullable and defined on Car table and with DeleteBehaviour.Cascade:

                entity.Property<int?>("PersonId").HasMaxLength(100).IsRequired(false);
                entity.HasOne<Person>()
                    .WithOne(p => p.Car)
                    .HasForeignKey<Car>("PersonId")
                    .OnDelete(DeleteBehavior.Cascade);

We want to be able to delete the child by setting parent reference to null:

                person.Car = null;

After ChangeTracker.DetectChanges() is called, the child is correctly marked as deleted but the reference itself is reloaded, so person.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 to null because of DeleteBehaviour.Cascade and the fact that subsequent ChangeDetector.DetectKeyChange() compares snapshot value (contains null) with actual value of underlying property (contains original key).

Steps to reproduce

Project:
https://github.com/xsmerek/EFCoreDeletingNullableReference

Steps:

  • run EFCoreDeletingNullableReference\setup.sql to create DB and data.
  • adjust connection sting in EFCoreDeletingNullableReferenceContext
  • Run test DeleteReferenceTest

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.

@ajcvickers
Copy link
Member

Verified that this is fixed in the latest daily build. Will add a test.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 7, 2022
ajcvickers added a commit that referenced this issue Jul 8, 2022
Fixes #27497

Also tests for already fixed issues:

Fixes #26937
Fixes #26827
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview7 Jul 10, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview7, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants