title: The instance of entity type ... cannot be tracked because another instance of this type with the same key is already being tracked
body: <p>Given an ASP.NET Core webapp using Entity Framework Core and an SQL database.</p>
  
  <p>An absolute simple action is throwing this exception when trying to update an entity in the database. First noticed by a bug report in production.</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; Edit(string id, [Bind("Group")] EditViewModel model)
  {
      if (id != model.Group.Id) return NotFound();
  
      if (!ModelState.IsValid) return View(model);
  
      _context.Update(model.Group);
      await _context.SaveChangesAsync();
  
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>Exception is thrown at the line: <code>_context.Update(model.Group);</code></p>
  
  <blockquote>
    <p>InvalidOperationException: The instance of entity type 'Group' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.</p>
  </blockquote>
  
  <p>Clearly there is no other instance. I was able to reproduce the exception in my development environment when I stopped the code with a breakpoint on that line and expanded the Results property of the <code>_context.Group</code> object:
  <a href="https://i.stack.imgur.com/vLQMD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vLQMD.png" alt="screenshot of expanded Results property of the _context.Group object"></a>
  It's understandable, that when expanding the Results, it loads the instance needed to be updated and that's why the exception is thrown. But what's about the deployed production environment?</p>
  
  <p>Thanks for the help!</p>
  
  <p><strong>UPDATE1</strong> <code>Group</code> model:</p>
  
  <pre><code>public class Group
  {
      [Display(Name = "ID")]
      public string Id { get; set; }
  
      public virtual Country Country { get; set; }
  
      [Required]
      [Display(Name = "Country")]
      [ForeignKey("Country")]
      public string CountryCode { get; set; }
  
      [Required]
      [Display(Name = "Name")]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><strong>UPDATE2</strong> Based on @Mithgroth's answer, I was able to override the function <code>_context.Update()</code> to not need try-catch every time I use it:</p>
  
  <pre><code>public interface IEntity 
  {
      string Id { get; }
  }
  
  public override EntityEntry&lt;TEntity&gt; Update&lt;TEntity&gt;(TEntity entity)
  {
      if (entity == null)
      {
          throw new System.ArgumentNullException(nameof(entity));
      }
  
      try
      {
          return base.Update(entity);
      }
      catch (System.InvalidOperationException)
      {
          var originalEntity = Find(entity.GetType(), ((IEntity)entity).Id);
          Entry(originalEntity).CurrentValues.SetValues(entity);
          return Entry((TEntity)originalEntity);
      }
  }
  </code></pre>