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

Stop attempting to do local DetectChanges when not snapshot change-tracking #22439

Merged
merged 1 commit into from
Sep 8, 2020
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: 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