Skip to content

Commit

Permalink
Stop attempting to do local DetectChanges when not snapshot change-tr…
Browse files Browse the repository at this point in the history
…acking (#22439)

Fixes #21835
  • Loading branch information
ajcvickers authored Sep 8, 2020
1 parent 3f54048 commit a289dcd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/EFCore/ChangeTracking/Internal/ChangeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ public virtual void DetectChanges(IStateManager stateManager)

foreach (var entry in stateManager.ToList()) // Might be too big, but usually _all_ entities are using Snapshot tracking
{
if (entry.EntityType.GetChangeTrackingStrategy() == ChangeTrackingStrategy.Snapshot
&& entry.EntityState != EntityState.Detached)
if (entry.EntityState != EntityState.Detached)
{
LocalDetectChanges(entry);
}
Expand Down Expand Up @@ -199,6 +198,11 @@ private void LocalDetectChanges(InternalEntityEntry entry)
{
var entityType = entry.EntityType;

if (entry.EntityType.GetChangeTrackingStrategy() != ChangeTrackingStrategy.Snapshot)
{
return;
}

foreach (var property in entityType.GetProperties())
{
if (property.GetOriginalValueIndex() >= 0
Expand Down
19 changes: 19 additions & 0 deletions test/EFCore.Tests/ChangeTracking/Internal/ChangeDetectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,25 @@ public void Skips_detection_of_scalar_property_change_for_notification_entities(
Assert.False(entry.IsModified(entry.EntityType.FindProperty("Name")));
}

[ConditionalFact]
public void Skips_local_detection_of_scalar_property_change_for_notification_entities()
{
var contextServices = InMemoryTestHelpers.Instance.CreateContextServices(BuildModelWithChanged());

var changeDetector = contextServices.GetRequiredService<IChangeDetector>();

var category = new CategoryWithChanged { Id = 1, Name = "Oculus Rift" };
var entry = contextServices.GetRequiredService<IStateManager>().GetOrCreateEntry(category);
entry.SetEntityState(EntityState.Unchanged);

category.Name = "Gear VR";

changeDetector.DetectChanges(entry);

Assert.Equal(EntityState.Unchanged, entry.EntityState);
Assert.False(entry.IsModified(entry.EntityType.FindProperty("Name")));
}

[ConditionalFact]
public void Detects_principal_key_change()
{
Expand Down

0 comments on commit a289dcd

Please sign in to comment.