Skip to content

Commit

Permalink
[release/6.0] Revert change to delete dependent when optional FK with…
Browse files Browse the repository at this point in the history
… cascade delete is set to null (#27216)
  • Loading branch information
ajcvickers committed Feb 2, 2022
1 parent ab6e5dc commit 0dbe678
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/EFCore/ChangeTracking/Internal/NavigationFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class NavigationFixer : INavigationFixer
private readonly bool _useOldBehavior26779
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue26779", out var enabled) && enabled;

private readonly bool _useOldBehavior27174
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27174", out var enabled) && enabled;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -504,6 +507,8 @@ var targetDependentEntry
}

if (newValue == null
&& (foreignKey.IsRequired
|| _useOldBehavior27174)
&& (foreignKey.DeleteBehavior == DeleteBehavior.Cascade
|| foreignKey.DeleteBehavior == DeleteBehavior.ClientCascade))
{
Expand Down
11 changes: 8 additions & 3 deletions test/EFCore.Tests/ChangeTracking/ChangeTrackerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ public void Dependent_FKs_are_not_nulled_when_principal_is_detached(bool delayCa
}
}

[ConditionalTheory] // Issues #16546 #25360
[ConditionalTheory] // Issues #16546 #25360; Change reverted in #27174.
[InlineData(false, false, false, true, false)]
[InlineData(true, false, false, true, false)]
[InlineData(false, true, false, true, false)]
Expand Down Expand Up @@ -2043,7 +2043,8 @@ public void Optional_relationship_with_cascade_still_cascades(
Assert.Equal(EntityState.Unchanged, context.Entry(attachedContainer).State);
Assert.Equal(EntityState.Unchanged, context.Entry(attachedTroduct).State);

if (delayCascade)
if (delayCascade
|| (useForeignKey && setProperty))
{
Assert.Equal(EntityState.Modified, context.Entry(attachedRoom).State);
}
Expand All @@ -2058,7 +2059,11 @@ public void Optional_relationship_with_cascade_still_cascades(
Assert.Equal(3, context.ChangeTracker.Entries().Count());
Assert.Equal(EntityState.Unchanged, context.Entry(attachedContainer).State);
Assert.Equal(EntityState.Unchanged, context.Entry(attachedTroduct).State);
Assert.Equal(EntityState.Deleted, context.Entry(attachedRoom).State);


Assert.Equal(
useForeignKey && setProperty ? EntityState.Modified : EntityState.Deleted,
context.Entry(attachedRoom).State);

context.SaveChanges();
}
Expand Down

0 comments on commit 0dbe678

Please sign in to comment.