title: The instance of entity type 'ApplicationUser' cannot be tracked because another instance with the same
body: <p>I have this error and I can't figure out why it is happening. Can someone help out?</p>
  <blockquote>
  <p>The instance of entity type 'ApplicationUser' 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 using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.</p>
  </blockquote>
  <p>I checked the previous answers and all of them say that somewhere another instance is being used. But I have simplified code to 2 lines in the controller. This must be somewhere else, but I don't know where to look.</p>
  <pre><code>{
      [Route(&quot;api/user&quot;)]
      [ApiController]
      public class ApplicationUsersController : Controller
      {
          private readonly IEmailService _emailService;
          private readonly IIdentityService _identityService;
          private readonly IDbRepository&lt;ApplicationUser&gt; _userRepository;
          private readonly IMapper _mapper;
          private readonly UserManager&lt;ApplicationUser&gt; _userManager;
          private readonly RoleManager&lt;IdentityRole&gt; _roleManager;
  
          private readonly IConfiguration _configuration;
  
          public ApplicationUsersController(
              IDbRepository&lt;ApplicationUser&gt; userRepository,
              IMapper mapper,
              UserManager&lt;ApplicationUser&gt; userManager,
              RoleManager&lt;IdentityRole&gt; roleManager,
              IEmailService emailService,
              IIdentityService identityService,
              IConfiguration configuration)
          {
              _userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository));
              _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
              _userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
              _roleManager = roleManager ?? throw new ArgumentNullException(nameof(roleManager));
              _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService));
              _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
              _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
          }
  
      [HttpPut(&quot;{userId}&quot;)]
      [Authorize(Roles = GlobalConstants.AdminRole + &quot;,&quot; + GlobalConstants.ManagerRole + &quot;,&quot; + GlobalConstants.AppraiserRole)]
      public async Task&lt;IActionResult&gt; UpdatePasswordAndEmail([FromBody] 
      UserViewModel model, [FromRoute] string userId)
      {
          var user = await _userRepository.All().FirstOrDefaultAsync(x=&gt;x.Id==userId);
          var res1 = await this._userManager.RemovePasswordAsync(user); // THIS LINE GIVES ERROR
          return Ok();
      }
  
  }
  </code></pre>
  <p>Any help appreciated</p>
  <p>I am registering the context as follows:</p>
  <pre><code>builder.RegisterType&lt;AmritaDbContext&gt;().As&lt;IAmritaDbContext&gt;().InstancePerLifetimeScope();
  
  builder.RegisterGeneric(typeof(DbRepository&lt;&gt;)).As(typeof(IDbRepository&lt;&gt;)).InstancePerLifetimeScope();`
  </code></pre>
  <p>Configure from startup:</p>
  <pre><code>    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
                  // app.UseBrowserLink();
              }
              else
              {
                  app.UseExceptionHandler(&quot;/Home/Error&quot;);
              }
  
  
              app.UseCors(&quot;CorsPolicy&quot;);
              app.UseIdentityServer();
              app.UseHttpsRedirection();
              var option = new RewriteOptions();
              option.AddRedirect(&quot;^$&quot;, &quot;swagger&quot;);
              app.UseRewriter(option);
              app.UseStaticFiles();
  
              
              ConfigureAuth(app);
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: &quot;default&quot;,
                      template: &quot;api/v1/{controller=Home}/{action=Index}/{id?}&quot;);
              });
  
              var pathBase = Configuration[&quot;PATH_BASE&quot;];
  
              app.UseSwagger()
                 .UseSwaggerUI(c =&gt;
                 {
                     c.SwaggerEndpoint($&quot;{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json&quot;, &quot;Amrita.API V1&quot;);
                     c.OAuthClientId(&quot;swaggerclient&quot;);
                     c.OAuthAppName(&quot;Amrita Swagger UI&quot;);
                 });
          }
  </code></pre>
  <p>Configure from Identity <code>Startup.cs</code>:</p>
  <pre><code> public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              // InitializeIdentityServerDatabase(app);
  
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseIdentityServer();
  
              app.UseStaticFiles();
              app.UseMvcWithDefaultRoute();
          }
  </code></pre>