Я получаю эту ошибку в этой строке:
var removeRole = await _userManager.RemoveFromRolesAsync(applicationUser, roles);
Вот код:
public async Task SaveClient(UserViewModel viewModel)
{
try
{
var applicationUser = await DbSet.SingleOrDefaultAsync(x => x.Id == viewModel.Id && !x.Deleted);
if (applicationUser == null) throw new Exception("User not found. ");
var roles = await _userManager.GetRolesAsync(applicationUser);
if (!(await _userManager.IsInRoleAsync(applicationUser, viewModel.Role)))
{
var removeRole = await _userManager.RemoveFromRolesAsync(applicationUser, roles);
var addRole = await _userManager.AddToRoleAsync(applicationUser, viewModel.Role);
if (viewModel.Role.Equals("Agente") && applicationUser.AgentId == null)
viewModel.AgentId = "A" + new Random().Next(999) + new Random().Next(999);
}
Mapper.Map(viewModel, applicationUser);
await Edit(applicationUser);
// return Task.CompletedTask;
}
catch (Exception ex)
{
throw new Exception(ex.Message + " User not found. ");
}
}
Я пытался "использовать" в строке "var applicationUser", но все еще нетудачи.
Любая помощь?
Это мои услуги класса запуска:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(config => {
config.SignIn.RequireConfirmedEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IInvestmentRepository, InvestmentRepository>();
services.AddTransient<IProjectRepository, ProjectRepository>();
services.AddTransient<IPortfolioRepository, PortfolioRepository>();
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IPaypalRepository, PaypalRepository>();
services.AddTransient<IStripeRepository, StripeRepository>();
services.AddTransient<IOrderRepository, OrderRepository>();
services.AddTransient<IComissionRepository, ComissionRepository>();
services.AddTransient<IWithdrawRepository, WithdrawRepository>();
services.AddTransient<IDepositRepository, DepositRepository>();
services.AddAutoMapper();
services.AddDistributedMemoryCache();
services.AddSession();
services.AddCors();
services.AddMvc();
}