What is the possible cause of this error:
InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager [Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
My target framework is netcoreapp2.1
.
This is my user store class:
public class MyUserStore : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
And my user role class:
public class MyUserRole : IdentityRole
{
public string Description { get; set; }
}
My DbContext:
public class ApplicationDbContext : IdentityDbContext<MyUserStore,MyUserRole,string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options): base(options) { }
}
My ConfigureServices
method in Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>()
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<MyUserStore, MyUserRole>(cfg => {
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<Seeder>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
i want to understand why this is happening and what is the best practice.