简体   繁体   English

无法跟踪实体类型“IdentityUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例

[英]The instance of entity type 'IdentityUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

I am using as.net core Identity to handle users, I am trying to update the password with the method ResetPasswordAsync() but I get the error message我正在使用 as.net core Identity 来处理用户,我正在尝试使用 ResetPasswordAsync() 方法更新密码,但我收到错误消息

The instance of entity type 'CustomUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.无法跟踪实体类型“CustomUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例。

CustomUser being a class that inherit from IdentityUser CustomUser 是继承自 IdentityUser 的 class

The reason of this error is probably because I am querying for the user before I update it, but I don't know what the problem is since I am using AsNoTracking().这个错误的原因可能是因为我在更新之前查询用户,但我不知道问题是什么,因为我使用的是 AsNoTracking()。 So I believe the problem might be something more related to the IdentityUser class.所以我认为问题可能与 IdentityUser class 更相关。

here is the code:这是代码:

public async Task<bool> UpdatePassword(int userId, string token, string password)
{
     var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId);
     var results = await _userManager.ResetPasswordAsync(user, token, password);
     return true;
}

code that is called before this one:在此之前调用的代码:

public async Task<bool> SendForgetPasswordEmail(string username) {
    var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.UserName == userName);
    var token = await _userManager.GeneratePasswordResetTokenAsync(user);
    var mailServiceVars = _configuration.GetSection("MailService");
    var usernameMail = mailServiceVars["Username"];
    var passwordMail = mailServiceVars["Password"];
    var mailService = new MailService("smtp.gmail.com", usernameMail, passwordMail);

    mailService.SendEmail(
        username,
        "Recover your Password",
        string.Concat("https://localhost:44374/resetPassword?token=", token),
        string.Empty
        );
    return true;
}

_context is a dbContext that inherits Entity's IdentityDbContext class. _context是一个dbContext,继承了Entity的IdentityDbContext class。

_userManager is a UserManager class from AspNetCore.Identity library. _userManager 是 AspNetCore.Identity 库中的 UserManager class。

_contex, _configuration and _userManager are initialized through dependency injection. _contex、_configuration 和_userManager 通过依赖注入进行初始化。

The Users table that I am accessing with _context was created with Identity library.我使用 _context 访问的用户表是使用身份库创建的。

please let me know if you have any more questions or if you need more.如果您还有其他问题或需要更多信息,请告诉我。 This is a simplified version of my code.这是我的代码的简化版本。

So it turns out I found a way to fix it.所以事实证明我找到了修复它的方法。 Just using _userManager instead of _context here:在这里只使用 _userManager 而不是 _context:

     var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId);

so instead the code would be所以代码将是

public async Task<bool> UpdatePassword(int userId, string token, string password)
{
     var user = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);
     var results = await _userManager.ResetPasswordAsync(user, token, password);
     return true;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“书签”的实例,因为已经在跟踪具有相同键值 {'ID'} 的另一个实例 - The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked 无法跟踪实体类型“Item”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型的实例,因为已跟踪另一个具有与 {'Id'} 相同键值对的实例 - The instance of the entity type cannot be tracked because another instance with the same key value pair for{'Id'} is already being tracked 实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例 - The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型 Model 的实例,因为已跟踪另一个具有相同 {&#39;Id&#39;} 键值的实例 - The instance of entity type Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“Bus”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例 - The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked 无法跟踪实体类型“AppUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“ Article”的实例,因为已经跟踪了另一个具有相同键值的{&#39;Id&#39;}实例 - The instance of entity type 'Article' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“用户”的实例,因为已经在跟踪另一个具有相同键值的“Id”实例 - The instance of entity type 'User' cannot be tracked because another instance with the same key value for 'Id' is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM