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>I have a Service Object <code>Update</code> </p>
  
  <pre><code>public bool Update(object original, object modified)
  {
      var originalClient = (Client)original;
      var modifiedClient = (Client)modified;
      _context.Clients.Update(originalClient); //&lt;-- throws the error
      _context.SaveChanges();
      //Variance checking and logging of changes between the modified and original
  }
  </code></pre>
  
  <p>This is where I am calling this method from:</p>
  
  <pre><code>public IActionResult Update(DetailViewModel vm)
  {
      var originalClient = (Client)_service.GetAsNoTracking(vm.ClientId);
      var modifiedClient = (Client)_service.Fetch(vm.ClientId.ToString());
      // Changing the modifiedClient here
      _service.Update(originalClient, modifiedClient);
  }
  </code></pre>
  
  <p>Here is the <code>GetAsNotTracking</code> method:</p>
  
  <pre><code>public Client GetAsNoTracking(long id)
  {
      return GetClientQueryableObject(id).AsNoTracking().FirstOrDefault();
  }
  </code></pre>
  
  <p><code>Fetch</code> method:</p>
  
  <pre><code>public object Fetch(string id)
  {
     long fetchId;
     long.TryParse(id, out fetchId);
     return GetClientQueryableObject(fetchId).FirstOrDefault();
  }
  </code></pre>
  
  <p><code>GetClientQueryableObject</code>:</p>
  
  <pre><code>private Microsoft.Data.Entity.Query.IIncludableQueryable&lt;Client, ActivityType&gt; GetClientQueryableObject(long searchId)
  {
      return _context.Clients
          .Where(x =&gt; x.Id == searchId)
          .Include(x =&gt; x.Opportunities)
          .ThenInclude(x =&gt; x.BusinessUnit)
          .Include(x =&gt; x.Opportunities)
          .ThenInclude(x =&gt; x.Probability)
          .Include(x =&gt; x.Industry)
          .Include(x =&gt; x.Activities)
          .ThenInclude(x =&gt; x.User)
          .Include(x =&gt; x.Activities)
          .ThenInclude(x =&gt; x.ActivityType);
   }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>I have looked the following articles / discussions. To no avail:<a href="https://github.com/aspnet/EntityFramework/issues/3839" rel="noreferrer">ASP.NET GitHub Issue 3839</a></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here are the changes to <code>GetAsNoTracking</code>:</p>
  
  <pre><code>public Client GetAsNoTracking(long id)
  {
      return GetClientQueryableObjectAsNoTracking(id).FirstOrDefault();
  }
  </code></pre>
  
  <p><code>GetClientQueryableObjectAsNoTracking</code>:</p>
  
  <pre><code>private IQueryable&lt;Client&gt; GetClientQueryableObjectAsNoTracking(long searchId)
  {
      return _context.Clients
          .Where(x =&gt; x.Id == searchId)
          .Include(x =&gt; x.Opportunities)
          .ThenInclude(x =&gt; x.BusinessUnit)
          .AsNoTracking()
          .Include(x =&gt; x.Opportunities)
          .ThenInclude(x =&gt; x.Probability)
          .AsNoTracking()
          .Include(x =&gt; x.Industry)
          .AsNoTracking()
          .Include(x =&gt; x.Activities)
          .ThenInclude(x =&gt; x.User)
          .AsNoTracking()
          .Include(x =&gt; x.Activities)
          .ThenInclude(x =&gt; x.ActivityType)
          .AsNoTracking();
  }
  </code></pre>