Repository navigation
Milestone
Description
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
Reacted by aspirinv, Mitchell Cookson, Yann Zahringer Ferrando, Jon Tvermose Nielsen, Pejman, foka and hectorli
Activity
shaulbehr commentedon Nov 21, 2019
Hello, has anyone noticed this? I'd say this is a pretty serious bug, no?
ajcvickers commentedon Nov 21, 2019
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 commentedon Nov 24, 2019
@shaulbehr It looks like a workaround might be to revert the change to the cascade timing using something like this:
Can you test if this workaround works for you?
shaulbehr commentedon Nov 25, 2019
@ajcvickers Yep, that workaround works. Thanks!
Prevent dependents from being deleted when principal is deleted
Prevent dependents from being deleted when principal is detached
Prevent dependents from being deleted when principal is detached
6 remaining items
ajcvickers commentedon Feb 15, 2020
@silvajpr This is approved for the 3.1.3 patch, tentatively scheduled for March.
Prevent dependents from being deleted when principal is detached
[release/3.1] Cherry-picking commits for for closely related detach/d…
YZahringer commentedon Mar 20, 2020
@ajcvickers do you have an ETA for 3.1.3 patch release?
ajcvickers commentedon Mar 20, 2020
@YZahringer It's still tentatively scheduled for March.
freerider7777 commentedon Apr 1, 2021
Is this fixed?