I have the following code:
var items = _context.DbSetName
.Where(c => ...)
.Include(c => c.NavigationProperty)
.AsNoTracking()
.ToList();
But when I want to update the main entity, I get an error that the entity related to the main entity can’t be tracked because an entity with the same id is already being tracked.
So I also tried to detach it manually before updating it and saving it the following way:
foreach (var item in items)
{
if (item.NavigationProperty != null)
{
_context.Entry(item.NavigationProperty).State = EntityState.Detached;
}
_context.DbSetName.Update(item);
}
await _context.SaveChangesAsync(ct);
But I still get the same exception.
The exception occurs during at the Update and not the at the SaveChanges method.
3