Skip to content

Detaching an entity results in related entities being deleted #18982

Closed

Description

@shaulbehr

EF Core 3.0.1

I insert an object along with related objects. Then I set the state of the object to "Detached". I then perform another call to .SaveChangesAsync().

Observed behavior: the related objects are deleted.
Expected behavior: the related objects should not be deleted; rather their tracking status should simply be set to "Detached".

(This is a breaking change since upgrading from EF Core 2.2.4)

Steps to reproduce

    public class Thing
    {
        [Key]
        public Guid ThingId { get; set; }
        [MaxLength(200)]
        public string Description { get; set; }
        public List<OwnedByThing> OwnedByThings { get; set; } = new List<OwnedByThing>();
    }

    public class OwnedByThing
    {
        [Key]
        public Guid OwnedByThingId { get; set; }
        public Guid ThingId { get; set; }
        public Thing Thing { get; set; }
        public int Value { get; set; }
    }

            await using var db = new MyDbContext();
            var thing = new Thing
            {
                Description = "Hello world thing",
                OwnedByThings = new List<OwnedByThing>
                {
                    new OwnedByThing {Value = 1},
                    new OwnedByThing {Value = 2}
                }
            };

            db.Things.Add(thing);
            await db.SaveChangesAsync();

            var thingCount = await db.Things.CountAsync(e => e.ThingId == thing.ThingId);
            Assert.AreEqual(1, thingCount, "Thing Count A");
            var ownedCount = await db.OwnedByThings.CountAsync(e => e.ThingId == thing.ThingId);
            Assert.AreEqual(2, ownedCount, "Owned count A");

            db.Entry(thing).State = EntityState.Detached;
            await db.SaveChangesAsync();
            
            thingCount = await db.Things.CountAsync(e => e.ThingId == thing.ThingId);
            Assert.AreEqual(1, thingCount, "Thing Count B");
            ownedCount = await db.OwnedByThings.CountAsync(e => e.ThingId == thing.ThingId);
            Assert.AreEqual(2, ownedCount, "Owned count B");

Result:

  Owned count B
  Expected: 2
  But was:  0

Further technical details

EF Core version: 3.0.1
Database provider: Npgsql
Target framework:.NET Core 3.0
Operating system: Windows 10 Pro
IDE: JetBrains Rider

Activity

  1. shaulbehr commented on Nov 21, 2019

    @shaulbehr
    Author

    Hello, has anyone noticed this? I'd say this is a pretty serious bug, no?

  2. ajcvickers commented on Nov 21, 2019

    @ajcvickers
    Contributor

    Note for triage: this still repros with 3.1. It looks like detaching the principal is causing cascade delete of the dependents rather than cascade detach.

    @shaulbehr We will investigate whether this is something we should patch.

  3. ajcvickers commented on Nov 24, 2019

    @ajcvickers
    Contributor

    @shaulbehr It looks like a workaround might be to revert the change to the cascade timing using something like this:

    public BloggingContext()
    {
        ChangeTracker.CascadeDeleteTiming = CascadeTiming.OnSaveChanges;
        ChangeTracker.DeleteOrphansTiming = CascadeTiming.OnSaveChanges;
    }

    Can you test if this workaround works for you?

  4. shaulbehr commented on Nov 25, 2019

    @shaulbehr
    Author

    @ajcvickers Yep, that workaround works. Thanks!

  5. added this to the 5.0.0 milestone on Nov 25, 2019
  6. self-assigned this
    on Nov 25, 2019
  7. added 2 commits that reference this issue on Dec 21, 2019
    072142d
    bd02802
  8. added a commit that references this issue on Dec 22, 2019
    f77c79b
  9. removed this from the 5.0.0 milestone on Feb 8, 2020
  10. 6 remaining items

  11. ajcvickers commented on Feb 15, 2020

    @ajcvickers
    Contributor

    @silvajpr This is approved for the 3.1.3 patch, tentatively scheduled for March.

  12. added 2 commits that reference this issue on Feb 15, 2020
    d4a490e
    859588d
  13. modified the milestones: 3.1.x, 3.1.3 on Feb 15, 2020
  14. YZahringer commented on Mar 20, 2020

    @YZahringer

    @ajcvickers do you have an ETA for 3.1.3 patch release?

  15. ajcvickers commented on Mar 20, 2020

    @ajcvickers
    Contributor

    @YZahringer It's still tentatively scheduled for March.

  16. freerider7777 commented on Apr 1, 2021

    @freerider7777

    Is this fixed?

  17. removed their assignment
    on Sep 1, 2024
  18. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions