title: The instance of the entity type cannot be tracked because another instance with the keyvalue is being tracked
body: <p>I am basically trying to implement CRUD using EntityFrameWork core and .Net core 3.1. I have an issue with my update operation where I am not able update the context with the modified value.
  I am using postman to initiate the request.</p>
  <p>As you can see in the code below, I am trying to check if that customer exist and if it does pass the modified object to the context.</p>
  <p><a href="https://i.stack.imgur.com/szolg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/szolg.png" alt="enter image description here" /></a></p>
  <p><strong>Function code</strong></p>
  <pre><code>   [FunctionName(&quot;EditCustomer&quot;)]
      public async Task&lt;IActionResult&gt; Run(
      [HttpTrigger(AuthorizationLevel.Anonymous,&quot;post&quot;, Route = &quot;update-customer&quot;)] HttpRequest req)
      {
          var customer = JsonConvert.DeserializeObject&lt;CustomerViewModel&gt;(new StreamReader(req.Body).ReadToEnd());
          await _repo.UpdateCustomer(customer);
          return new OkResult();
      }
  </code></pre>
  <p><strong>Repository method</strong></p>
  <pre><code>  public async Task UpdateCustomer(CustomerViewModel customerViewModel)
      {
          if (customerViewModel.CustomerId != null)
          {
              var customer = _context.Customers.Where(c =&gt; c.CustomerId.Equals(customerViewModel.CustomerId)).FirstOrDefault();
  
              if (customer == null)
              {
                  throw new Exception(&quot;customer not found&quot;);
              }
              else
              {
                  _context.Customers.Update(_mapper.Map&lt;Customers&gt;(customerViewModel));
                  await _context.SaveChangesAsync();
  
              }
          }
         
      }
  </code></pre>
  <p>Mapping</p>
  <pre><code>   public class CustomerManagerProfile : Profile
      {
          public CustomerManagerProfile()
          {
              CreateMap&lt;CustomerDetails, CustomerDetailsViewModel&gt;().ReverseMap();
              CreateMap&lt;CustomerOrders, CustomerOrdersViewModel&gt;().ReverseMap();
              CreateMap&lt;CustomerOrderDetails, OrderDetailsViewModel&gt;().ReverseMap();
              CreateMap&lt;Customers, CustomerViewModel&gt;().ReverseMap();
          }
      }
  </code></pre>
  <p><strong>Solution</strong></p>
  <pre><code>public async Task UpdateCustomer(CustomerViewModel customerViewModel)
      {
          if (customerViewModel.CustomerId != null)
          {
              var customer = _context.Customers.Where(c =&gt; c.CustomerId.Equals(customerViewModel.CustomerId)).FirstOrDefault();
  
              if (customer == null)
              {
                  throw new Exception(&quot;customer not found&quot;);
              }
              else
              {
                  var customerModel = _mapper.Map&lt;Customers&gt;(customerViewModel);
                  _context.Entry&lt;Customers&gt;(customer).State = EntityState.Detached;
                  _context.Entry&lt;Customers&gt;(customerModel).State = EntityState.Modified;
                  await _context.SaveChangesAsync();
  
              }
          }
  }
    
  
       
  </code></pre>