title: Getting 'The instance of entity type cannot be tracked because another instance with same key value for is already being tracked' with AsNoTracking()
body: <p>I'm creating my Telegram bot using Entity Framework Core. I have these models:</p>
  <pre><code>public class User
  {
      public int Id { get; set; }
      public long UserId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string UserName { get; set; }
      public ICollection&lt;Chat&gt; Chats { get; set;} = new List&lt;Chat&gt;();
      public ICollection&lt;Team&gt; CreatedTeams { get; set; } = new List&lt;Team&gt;();
      public ICollection&lt;Team&gt; Teams { get; set; } = new List&lt;Team&gt;();
  }
  
  public class Team
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int? ChatId { get; set; }
      public Chat Chat { get; set; }
      public int? CreatorId { get; set; }
      public User Creator { get; set; }
      public ICollection&lt;User&gt; Users { get; set; } = new List&lt;User&gt;();
  }
  </code></pre>
  <p>When I handle incoming messages, I check if a <code>User</code> with the same <code>UserId</code> exists in DB. If it exists, then I just get it, if not - create new one on a base of <code>Telegram User</code> and return it.</p>
  <pre><code>public async Task&lt;User&gt; GetNewOrExistingUserAsync(Telegram.Bot.Types.User telegramUser)
  {
      var user = this.GetUserByUserId(telegramUser.Id);
  
      if (user is null)
      {
          await this.userRepository.AddAsync((User)telegramUser);
          return this.GetUserByUserId(telegramUser.Id);
      }
  
      return user;
  }
  
  private User GetUserByUserId(long userId)
  {
      return this.userRepository.GetAll()
              .Include(user =&gt; user.Chats)
              .Include(user =&gt; user.Teams)
              .Include(user =&gt; user.CreatedTeams)
              .AsNoTracking()
              .FirstOrDefault(user =&gt; user.UserId == userId);
   }
  </code></pre>
  <p>After that I get <code>Team</code> by name specified in message:</p>
  <pre><code>public Team GetChatTeamByName(string name, long chatId)
  {
      return this.teamRepository.GetAll()
           .Include(team =&gt; team.Creator)
           .Include(team =&gt; team.Users)
           .AsNoTracking()
           .FirstOrDefault(team =&gt; team.Chat.ChatId == chatId &amp;&amp; team.Name.Equals(name));
  }
  </code></pre>
  <p>And finally I add <code>user</code> to <code>team</code> (I check, if it already exists in <code>team</code>, but I receive this error even without this check):</p>
  <pre><code>team.Users.Add(user);
  await this.teamRepository.UpdateAsync(team);
  </code></pre>
  <p>Here I get this error: '<em>The instance of entity type 'User' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked</em>'. <code>UpdateAsync()</code> uses <code>Entities.Update()</code> method and saves changes. Error occurs after <code>Entities.Update()</code> call. I do not understand, where it starts tracking user - as you can see, I use <code>AsNoTracking()</code> in all related queries. I could assume, that it starts tracking after adding new user, but error occurs even when user is already in DB.</p>
  <p>I've tried to use <code>Entry&lt;User&gt;(user).State = EntityState.Detached;</code> as people suggest, right before adding user to team, or after it, but error is the same. The context in my ASP app is <code>Transient</code>, but this error occurs even if it <code>Scoped</code>. Could you help me please with it? Or are there any other ways to add <code>user</code> to <code>team</code> without calling <code>Update()</code> method?</p>
  <p>The whole method is:</p>
  <pre><code>public async Task&lt;Message&gt; AddUserToTeamAsync(ITelegramBotClient botClient, Message message)
  {
      var user = await this.userService.GetNewOrExistingUserAsync(message.From);
  
      var teamName = message.Text.SplitToWords()[1];
      var team = this.teamService.GetChatTeamByName(teamName, message.Chat.Id);
  
      team.Users.Add(user);
      await this.teamRepository.UpdateAsync(team);
      .....
  }
  </code></pre>
  <p>Update.<code>GetAll()</code> method:</p>
  <pre><code>public virtual IQueryable&lt;T&gt; GetAll()
  {
      return this.Entities;
  }
  </code></pre>