1

I have some code that worked fine in EF Core 2.0. But when I upgrade to EF 3.1, I am getting an error:

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

In the screenshot below, I am updating Id (PK) 585 entity while Id 1822 and 1829 are Unchanged but when I update 1822 I am getting above error.

I have this code before updating:

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;

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • Does this answer your question? [The instance of entity type 'FileRepo' cannot be tracked because another instance with the same key value for {'FileID'} is already being tracked](https://stackoverflow.com/questions/68271940/the-instance-of-entity-type-filerepo-cannot-be-tracked-because-another-instanc) – Svyatoslav Danyliv Jan 16 '22 at 11:30
  • `Attaching will fail if there is already attached object with the same key.` I have different Id – Imran Qadir Baksh - Baloch Jan 16 '22 at 12:43
  • It is very strange because the entity is detached and not contained in changeTracker. But to solve it you can use 'AsNoTracking()' when fetching the data or you can try to get the entity from context instead of attaching a new one while updating it. – Navid Rsh Jan 16 '22 at 12:49
  • @NavidRsh Entity is not detached but I am updating other entity. But there should be some breaking change between EF Core 2.0 and 3.1 – Imran Qadir Baksh - Baloch Jan 16 '22 at 13:00
  • Attach also may fail when you reuse the same object instance. It is not your case? – Svyatoslav Danyliv Jan 16 '22 at 13:56
  • This detaching code indicates that you're working with contexts having too long life cycles. It seems to me that that is the first issue you should tackle. If your `Update` method would receive a brand new context instance you wouldn't have this issue. BTW, the `Attach` line is redundant. – Gert Arnold Jan 16 '22 at 13:59
  • @GertArnold unfortunately Attach is part of generic repository – Imran Qadir Baksh - Baloch Jan 16 '22 at 17:45
  • Well, the context life cycle issue is far more important. – Gert Arnold Jan 16 '22 at 18:35

1 Answers1

0

I have added one more Unchanged entity as well as mentioned in this comment, 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;
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322