InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyWebsite.Models.User]' while attempting to activate 'MyWebsite.Controllers.AccountController'.
I get this error when accessing / Account / Register
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
//services.AddIdentity<User, IdentityRole>()
//.AddEntityFrameworkStores<ApplicationDbContext>();0
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
Class User
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}
Account Controller
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}
I'm new, help
You are adding a service for services.AddDefaultIdentity<IdentityUser>()
When you have created a new User
Model
So you have to add the service for that Model services.AddDefaultIdentity<User>()
Edit
Making sure you have the IdentityRoles
service working
services.AddDefaultIdentity<User, IdentityRole>()