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

Don't call local DetectChanges in cascade processing if AutoDetectChangesEnabled is false #28402

Merged
merged 1 commit into from
Jul 12, 2022
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
8 changes: 7 additions & 1 deletion src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,9 @@ public virtual void CascadeDelete(InternalEntityEntry entry, bool force, IEnumer
var doCascadeDelete = force || CascadeDeleteTiming != CascadeTiming.Never;
var principalIsDetached = entry.EntityState == EntityState.Detached;

var detectChangesEnabled = Context.ChangeTracker.AutoDetectChangesEnabled
&& !((IRuntimeModel)Model).SkipDetectChanges;

foreignKeys ??= entry.EntityType.GetReferencingForeignKeys();
foreach (var fk in foreignKeys)
{
Expand All @@ -1103,7 +1106,10 @@ public virtual void CascadeDelete(InternalEntityEntry entry, bool force, IEnumer
continue;
}

ChangeDetector.DetectChanges(dependent);
if (detectChangesEnabled)
{
ChangeDetector.DetectChanges(dependent);
}

if (dependent.EntityState != EntityState.Deleted
&& dependent.EntityState != EntityState.Detached
Expand Down
35 changes: 35 additions & 0 deletions test/EFCore.Tests/DbContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,41 @@ protected internal override void OnConfiguring(DbContextOptionsBuilder optionsBu
.UseInternalServiceProvider(_serviceProvider);
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void DetectChanges_is_called_for_cascade_delete_unless_disabled(bool autoDetectChangesEnabled)
{
var detectedChangesFor = new List<object>();

using var context = new EarlyLearningCenter();
context.ChangeTracker.DetectingEntityChanges += (_, args) =>
{
detectedChangesFor.Add(args.Entry.Entity);
};

context.ChangeTracker.AutoDetectChangesEnabled = autoDetectChangesEnabled;

var products = new List<Product> { new() { Id = 1 }, new() { Id = 2 } };
var category = context.Attach(new Category { Id = 1, Products = products }).Entity;

Assert.Empty(detectedChangesFor);

context.Remove(category);

if (autoDetectChangesEnabled)
{
Assert.Equal(4, detectedChangesFor.Count);
Assert.Contains(products[0], detectedChangesFor);
Assert.Contains(products[1], detectedChangesFor);
Assert.Equal(2, detectedChangesFor.Count(e => ReferenceEquals(e, category)));
}
else
{
Assert.Empty(detectedChangesFor);
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
Expand Down