升级到 EF 3.1 后更新影响未更改实体的实体

Updating Entity impacting unchanged entity after upgrade to EF 3.1

我有一些代码在 EF Core 2.0 中运行良好。但是当我升级到 EF 3.1 时,出现错误:

The instance of entity type 'ChildEntity' cannot be tracked because another instance with the key value '{Id: 1822}' is already being tracked` error

在下面的屏幕截图中,我正在更新 Id (PK) 585 实体,而 Id 1822 和 1829 未更改,但是当我更新 1822 时出现上述错误。

更新前我有这个代码:

var changedEntriesCopy = _context.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added ||
                        e.State == EntityState.Modified ||
                        e.State == EntityState.Deleted)
            .ToList();

foreach (var entry in changedEntriesCopy)
    entry.State = EntityState.Detached;

我又添加了一个未更改的实体以及此评论中提到的,How do I clear tracked entities in entity framework

var changedEntriesCopy = _context.ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added ||
                        e.State == EntityState.Modified ||
                        e.State == EntityState.Deleted ||
                        e.State == EntityState.Unchanged)
            .ToList();

foreach (var entry in changedEntriesCopy)
    entry.State = EntityState.Detached;