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

Undelete an entity when it has been orphaned and then reparented #25574

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public virtual void NavigationReferenceChanged(

// Set the FK properties to reflect the change to the navigation.
SetForeignKeyProperties(entry, newTargetEntry, foreignKey, setModified: true, fromQuery: false);
UndeleteDependent(entry, newTargetEntry);
entry.SetRelationshipSnapshotValue(navigation, newValue);
}
}
Expand Down Expand Up @@ -231,7 +232,7 @@ public virtual void NavigationReferenceChanged(
}

SetForeignKeyProperties(newTargetEntry, entry, foreignKey, setModified: true, fromQuery: false);

UndeleteDependent(entry, newTargetEntry);
SetNavigation(newTargetEntry, inverse, entry, fromQuery: false);
}
}
Expand Down Expand Up @@ -363,6 +364,7 @@ public virtual void NavigationCollectionChanged(

// Set the FK properties on added dependents to match this principal
SetForeignKeyProperties(newTargetEntry, entry, foreignKey, setModified: true, fromQuery: false);
UndeleteDependent(newTargetEntry, entry);

// Set the inverse navigation to point to this principal
SetNavigation(newTargetEntry, inverse, entry, fromQuery: false);
Expand Down Expand Up @@ -507,6 +509,7 @@ var targetDependentEntry
.GetDependentsUsingRelationshipSnapshot(entry, foreignKey).ToList())
{
SetForeignKeyProperties(dependentEntry, entry, foreignKey, setModified: true, fromQuery: false);
UndeleteDependent(dependentEntry, entry);
}

if (foreignKey.IsOwnership)
Expand Down Expand Up @@ -1220,6 +1223,18 @@ private static void SetForeignKeyProperties(
}
}

private static void UndeleteDependent(
InternalEntityEntry dependentEntry,
InternalEntityEntry principalEntry)
{
if (dependentEntry.EntityState == EntityState.Deleted
&& (principalEntry.EntityState == EntityState.Unchanged
|| principalEntry.EntityState == EntityState.Modified))
{
dependentEntry.SetEntityState(EntityState.Modified);
}
}

private static bool PrincipalValueEqualsDependentValue(
IProperty principalProperty,
object? dependentValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,244 @@ public virtual void Reparent_to_different_one_to_many(
});
}

[ConditionalTheory]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Principal, null)]
[InlineData((int)ChangeMechanism.Dependent, null)]
[InlineData((int)ChangeMechanism.Fk, null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), null)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), null)]
public virtual void Reparent_dependent_one_to_many(
ChangeMechanism changeMechanism,
CascadeTiming? deleteOrphansTiming)
{
Required1 oldParent = null;
Required1 newParent = null;
Required2 child = null;

ExecuteWithStrategyInTransaction(
context =>
{
context.ChangeTracker.DeleteOrphansTiming = deleteOrphansTiming ?? CascadeTiming.Never;

var root = LoadRequiredGraph(context);

oldParent = root.RequiredChildren.OrderBy(e => e.Id).First();
newParent = root.RequiredChildren.OrderBy(e => e.Id).Last();
child = oldParent.Children.OrderBy(e => e.Id).Skip(1).First();

if ((changeMechanism & ChangeMechanism.Principal) != 0)
{
((ICollection<Required2>)oldParent.Children).Remove(child);
((ICollection<Required2>)newParent.Children).Add(child);
}

if ((changeMechanism & ChangeMechanism.Dependent) != 0)
{
child.Parent = newParent;
}

if ((changeMechanism & ChangeMechanism.Fk) != 0)
{
child.ParentId = newParent.Id;
}

if (!Fixture.ForceClientNoAction
|| deleteOrphansTiming != CascadeTiming.Immediate
|| (changeMechanism & ChangeMechanism.Fk) != 0
|| changeMechanism == ChangeMechanism.Dependent)
{
Assert.True(context.ChangeTracker.HasChanges());

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.Id, child.ParentId);
Assert.Equal(EntityState.Modified, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);

context.SaveChanges();

Assert.False(context.ChangeTracker.HasChanges());

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.Id, child.ParentId);
Assert.Equal(EntityState.Unchanged, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);
}
else
{
Assert.Throws<InvalidOperationException>(() => context.ChangeTracker.DetectChanges());
}
},
context =>
{
if (!Fixture.ForceClientNoAction
|| deleteOrphansTiming != CascadeTiming.Immediate
|| (changeMechanism & ChangeMechanism.Fk) != 0
|| changeMechanism == ChangeMechanism.Dependent)
{
var root = LoadRequiredGraph(context);

Assert.False(context.ChangeTracker.HasChanges());

oldParent = root.RequiredChildren.First(e => e.Id == oldParent.Id);
newParent = root.RequiredChildren.First(e => e.Id == newParent.Id);
child = newParent.Children.First(e => e.Id == child.Id);

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.Id, child.ParentId);
Assert.Equal(EntityState.Unchanged, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);
}
});
}

[ConditionalTheory]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.OnSaveChanges)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.Immediate)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.Immediate)]
[InlineData((int)ChangeMechanism.Principal, CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Dependent, CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Fk, CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), CascadeTiming.Never)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), CascadeTiming.Never)]
[InlineData((int)ChangeMechanism.Principal, null)]
[InlineData((int)ChangeMechanism.Dependent, null)]
[InlineData((int)ChangeMechanism.Fk, null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent), null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Fk), null)]
[InlineData((int)(ChangeMechanism.Fk | ChangeMechanism.Dependent), null)]
[InlineData((int)(ChangeMechanism.Principal | ChangeMechanism.Dependent | ChangeMechanism.Fk), null)]
public virtual void Reparent_dependent_one_to_many_ak(
ChangeMechanism changeMechanism,
CascadeTiming? deleteOrphansTiming)
{
RequiredAk1 oldParent = null;
RequiredAk1 newParent = null;
RequiredAk2 child = null;

ExecuteWithStrategyInTransaction(
context =>
{
context.ChangeTracker.DeleteOrphansTiming = deleteOrphansTiming ?? CascadeTiming.Never;

var root = LoadRequiredAkGraph(context);

oldParent = root.RequiredChildrenAk.OrderBy(e => e.Id).First();
newParent = root.RequiredChildrenAk.OrderBy(e => e.Id).Last();
child = oldParent.Children.OrderBy(e => e.Id).Skip(1).First();

if ((changeMechanism & ChangeMechanism.Principal) != 0)
{
((ICollection<RequiredAk2>)oldParent.Children).Remove(child);
((ICollection<RequiredAk2>)newParent.Children).Add(child);
}

if ((changeMechanism & ChangeMechanism.Dependent) != 0)
{
child.Parent = newParent;
}

if ((changeMechanism & ChangeMechanism.Fk) != 0)
{
child.ParentId = newParent.AlternateId;
}

if (!Fixture.ForceClientNoAction
|| deleteOrphansTiming != CascadeTiming.Immediate
|| (changeMechanism & ChangeMechanism.Fk) != 0
|| changeMechanism == ChangeMechanism.Dependent)
{
Assert.True(context.ChangeTracker.HasChanges());

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.AlternateId, child.ParentId);
Assert.Equal(EntityState.Modified, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);

context.SaveChanges();

Assert.False(context.ChangeTracker.HasChanges());

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.AlternateId, child.ParentId);
Assert.Equal(EntityState.Unchanged, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);
}
else
{
Assert.Throws<InvalidOperationException>(() => context.ChangeTracker.DetectChanges());
}
},
context =>
{
if (!Fixture.ForceClientNoAction
|| deleteOrphansTiming != CascadeTiming.Immediate
|| (changeMechanism & ChangeMechanism.Fk) != 0
|| changeMechanism == ChangeMechanism.Dependent)
{
var root = LoadRequiredAkGraph(context);

Assert.False(context.ChangeTracker.HasChanges());

oldParent = root.RequiredChildrenAk.First(e => e.Id == oldParent.Id);
newParent = root.RequiredChildrenAk.First(e => e.Id == newParent.Id);
child = newParent.Children.First(e => e.Id == child.Id);

Assert.DoesNotContain(child, oldParent.Children);
Assert.Contains(child, newParent.Children);
Assert.Equal(newParent.AlternateId, child.ParentId);
Assert.Equal(EntityState.Unchanged, context.Entry(child).State);
Assert.Equal(EntityState.Unchanged, context.Entry(oldParent).State);
Assert.Equal(EntityState.Unchanged, context.Entry(newParent).State);
}
});
}

[ConditionalTheory]
[InlineData((int)ChangeMechanism.Principal, false, CascadeTiming.OnSaveChanges)]
[InlineData((int)ChangeMechanism.Principal, true, CascadeTiming.OnSaveChanges)]
Expand Down