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

Detaching an entity results in related entities being deleted #18982

Closed
shaulbehr opened this issue Nov 19, 2019 · 9 comments · Fixed by #19379
Closed

Detaching an entity results in related entities being deleted #18982

shaulbehr opened this issue Nov 19, 2019 · 9 comments · Fixed by #19379
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@shaulbehr
Copy link

shaulbehr commented Nov 19, 2019

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

@shaulbehr
Copy link
Author

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

@ajcvickers
Copy link
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.

@ajcvickers
Copy link
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?

@shaulbehr
Copy link
Author

@ajcvickers Yep, that workaround works. Thanks!

@ajcvickers ajcvickers added this to the 5.0.0 milestone Nov 25, 2019
@ajcvickers ajcvickers self-assigned this Nov 25, 2019
@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 Dec 21, 2019
ajcvickers added a commit that referenced this issue Dec 21, 2019
ajcvickers added a commit that referenced this issue Dec 22, 2019
ajcvickers added a commit that referenced this issue Dec 22, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, 3.1.x Feb 8, 2020
@ajcvickers ajcvickers reopened this Feb 8, 2020
@silvajpr
Copy link

silvajpr commented Feb 12, 2020

Please fix this soon as possible!
I was not aware of this problem and I already lost data because of this problem!
This is a way to work!
I shouldn’t have to came here to know that you already found the problem 3 months ago and simply post an workaround instead of fixing it and relesse the fix in 3.1 or 3.1.1 etc.
I thought you were having short release cycles so you could deliver this kind of fixes fast!
Nothing in the breaking changes tell us about this side effect of the detach!
I am very frustated with this.

For me the Expected behavior is the related objects should not be deleted; rather their tracking status should REMAIN the same - it shouldn’t be affected - it’s the beahvior of EF6 and EFCore 2.2.
Exemple: if the related object state is Added it should continue with that state!

Thanks

ajcvickers added a commit that referenced this issue Feb 13, 2020
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
@ajcvickers
Copy link
Contributor

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

ajcvickers added a commit that referenced this issue Feb 15, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
@ajcvickers ajcvickers modified the milestones: 3.1.x, 3.1.3 Feb 15, 2020
@YZahringer
Copy link

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

@ajcvickers
Copy link
Contributor

@YZahringer It's still tentatively scheduled for March.

@freerider7777
Copy link

Is this fixed?

@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants