title: The instance of entity type 'Bot' cannot be tracked because another instance with the same key value for {'Id'}
body: <blockquote>
    <p>The instance of entity type 'Bot' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider us...</p>
  </blockquote>
  
  <p>I know what this problem means. It is happening right here <code>_context.Bots.Update(bot);</code>.</p>
  
  <p>The question is: is that a good way to solve this by adding <code>.AsNoTracking()</code> to all GetByXXX methods? Any suggestions?</p>
  
  <pre><code>[HttpPut("{id}")]
  public async Task&lt;ActionResult&gt; UpdateAsync([FromRoute] int id, [FromBody] BotCreateUpdateDto botCreateUpdateDto)
  {
      if (id != botCreateUpdateDto.Id)
      {
          return BadRequest(new { Error = "Invalid ID." });
      }
  
      var user = await _userService.GetByEmailAsync(botCreateUpdateDto.Email);
  
      if (user == null)
      {
          return BadRequest(new { Error = "Invalid e-mail." });
      }
  
      var cryptoPair = await _cryptoPairService.GetBySymbolAsync(botCreateUpdateDto.Symbol);
  
      if (cryptoPair == null)
      {
          return BadRequest(new { Error = "Invalid crypto pair." });
      }
  
      var timeInterval = await _timeIntervalService.GetByIntervalAsync(botCreateUpdateDto.Interval);
  
      if (timeInterval == null)
      {
          return BadRequest(new { Error = "Invalid time interval." });
      }
  
      var bot = new Bot
      {
          Id = botCreateUpdateDto.Id,
          Name = botCreateUpdateDto.Name,
          Status = botCreateUpdateDto.Status,
          UserId = user.Id,
          CryptoPairId = cryptoPair.Id,
          TimeIntervalId = timeInterval.Id
      };
  
      bool updated;
  
      try
      {
          updated = await _botService.UpdateAsync(bot);
      }
      catch (Exception ex)
      {
          return BadRequest(new { Error = ex.Message });
      }
  
      if (updated)
      {
          return NoContent();
      }
  
      return NotFound();
  }
  </code></pre>
  
  <pre><code>public async Task&lt;User&gt; GetByEmailAsync(string email)
  {
      return await _context.Users
          .Include(e =&gt; e.UserRoles)
              .ThenInclude(e =&gt; e.Role)
          .Include(e =&gt; e.Bots)
          .SingleOrDefaultAsync(e =&gt; e.Email == email);
  }
  </code></pre>
  
  <pre><code>public async Task&lt;CryptoPair&gt; GetBySymbolAsync(string symbol)
  {
      return await _context.CryptoPairs
          .Include(e =&gt; e.Bots)
          .AsNoTracking()
          .SingleOrDefaultAsync(e =&gt; e.Symbol == symbol);
  }
  </code></pre>
  
  <pre><code>public async Task&lt;TimeInterval&gt; GetByIntervalAsync(KlineInterval interval)
  {
      return await _context.TimeIntervals
          .Include(e =&gt; e.Bots)
          .AsNoTracking()
          .SingleOrDefaultAsync(e =&gt; e.Interval == interval);
  }
  </code></pre>
  
  <pre><code>public async Task&lt;bool&gt; UpdateAsync(Bot bot)
  {
      _context.Bots.Update(bot);
      var updated = await _context.SaveChangesAsync();
      return updated &gt; 0;
  }
  </code></pre>