Skip to content

Commit

Permalink
Stop returning detached entries from the ChangeTracker
Browse files Browse the repository at this point in the history
Issue #8921
  • Loading branch information
ajcvickers committed Jun 30, 2017
1 parent 0024373 commit 333f16a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ private IIdentityMap FindIdentityMap(IKey key)
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual IEnumerable<InternalEntityEntry> Entries => _entityReferenceMap.Values
.Concat(_dietReferenceMap.Values.SelectMany(e => e.Values));
.Concat(_dietReferenceMap.Values.SelectMany(e => e.Values))
.Where(e => e.EntityState != EntityState.Detached);

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
Expand Down
24 changes: 24 additions & 0 deletions test/EFCore.Tests/ChangeTracking/EntityEntryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ protected internal override void OnModelCreating(ModelBuilder modelBuilder)
}
}

[Fact]
public void Detached_entities_are_not_returned_from_the_change_tracker()
{
using (var context = new FreezerContext())
{
var entity = new Chunky { Id = 808 };
context.Attach(entity);

Assert.Equal(1, context.ChangeTracker.Entries().Count());

context.Entry(entity).State = EntityState.Detached;

Assert.Equal(0, context.ChangeTracker.Entries().Count());

context.ChangeTracker.DetectChanges();

Assert.Equal(0, context.ChangeTracker.Entries().Count());

context.Entry(entity);

Assert.Equal(0, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public void Can_obtain_entity_instance()
{
Expand Down
19 changes: 15 additions & 4 deletions test/EFCore.Tests/ChangeTracking/Internal/StateManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,21 @@ public void Can_get_all_entities()
var productId1 = new Guid("984ade3c-2f7b-4651-a351-642e92ab7146");
var productId2 = new Guid("0edc9136-7eed-463b-9b97-bdb9648ab877");

stateManager.StartTracking(stateManager.GetOrCreateEntry(new Category { Id = 77, PrincipalId = 777 }));
stateManager.StartTracking(stateManager.GetOrCreateEntry(new Category { Id = 78, PrincipalId = 778 }));
stateManager.StartTracking(stateManager.GetOrCreateEntry(new Product { Id = productId1 }));
stateManager.StartTracking(stateManager.GetOrCreateEntry(new Product { Id = productId2 }));
stateManager.StartTracking(
stateManager.GetOrCreateEntry(new Category { Id = 77, PrincipalId = 777 }))
.SetEntityState(EntityState.Unchanged);

stateManager.StartTracking(
stateManager.GetOrCreateEntry(new Category { Id = 78, PrincipalId = 778 }))
.SetEntityState(EntityState.Unchanged);

stateManager.StartTracking(
stateManager.GetOrCreateEntry(new Product { Id = productId1 }))
.SetEntityState(EntityState.Unchanged);

stateManager.StartTracking(
stateManager.GetOrCreateEntry(new Product { Id = productId2 }))
.SetEntityState(EntityState.Unchanged);

Assert.Equal(4, stateManager.Entries.Count());

Expand Down

0 comments on commit 333f16a

Please sign in to comment.