title: System.InvalidOperationException in EF core when trying to update
body: <h2>Problem</h2>
  <p>I am creating a web app in asp.net core with ef core using with Repository pattern. Ii am trying to insert an entity it works fine when i trying to check whether it is already exist in db or then if yes than update the same entity in same api it gives me this error.</p>
  <blockquote>
  <p>System.InvalidOperationException: 'The instance of entity type 'Consultation' 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>
  <h2>Image</h2>
  <p><a href="https://i.stack.imgur.com/LMUjz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LMUjz.png" alt="enter image description here" /></a></p>
  <h2>code</h2>
  <h3>insert api</h3>
  <pre><code>[HttpPost]
  public ApiResponse InsertConsultation([FromBody] Consultation consultation)
  {
      if (!ModelState.IsValid)
      {
          return new ApiResponse(StatusCodes.Status400BadRequest, error: &quot;error&quot;);
      }
  
      var consult = _consultationService.GetConsultationById(consultation.Id);
  
      if (consult !=null)
      {
          _consultationService.UpdateConsultation(consultation);
          return new ApiResponse(StatusCodes.Status200OK, success: &quot;isSuccess&quot;);
      }
  
      _consultationService.InsertConsultation(consultation);
      return new ApiResponse(StatusCodes.Status201Created, success: &quot;isSuccess&quot;);
  }
  </code></pre>