0

after searching and experimenting i still get the error cannot be tracked because another instance even when i have disabled the tracking.....

this is my code

    var result = _customerContext.Set<DO_Customer>()
              .AsNoTracking()
              .Where(x => x.Id == toUpdate.Id).FirstOrDefault();



                if (result != null)
                {
                    _customerContext.Entry(result).CurrentValues.SetValues(toUpdate);
                    _customerContext.Entry(result).State = EntityState.Modified;
                    foreach (var item in toUpdate.Address)
                    {
                        _customerContext.Entry(item).CurrentValues.SetValues(toUpdate);

                        _customerContext.Entry(result).State = EntityState.Modified;

                    }
}

            await _customerContext.SaveChangesAsync();

Anyone know why AsNoTracking is not working?

i have try everything. Only when i add no tracking in the program.cs it seem to work but thats other code and don't want to disable it fully

Teckniel
  • 15
  • 2
  • 1
    Does this answer your question? [The instance of entity type cannot be tracked because another instance of this type with the same key is already being tracked](https://stackoverflow.com/questions/36856073/the-instance-of-entity-type-cannot-be-tracked-because-another-instance-of-this-t) – nbk Jun 13 '23 at 22:17

1 Answers1

0

Check my answer on this Thread

Try

_context.ChangeTracker.Clear()
sabsab
  • 1,073
  • 3
  • 16
  • 30