Dmitry Vasilyuk Just3F
Dmitry Vasilyuk Just3F

Reputation: 187

.net core identity RoleManager exception

I create new class for change id type:

public sealed class Role : IdentityRole<long>
{

}

In AppContext it looks like:

builder.Entity<Role>(i => {
    i.ToTable("tblRole");
    i.HasKey(x => x.Id);
});

But when i try to create object of class RoleManager:

RoleManager<Role> roleManager = serviceProvider.GetRequiredService<RoleManager<Role>>();

I have this exception:

InvalidOperationException: A parameterless constructor was not found on entity type 'Role'. In order to create an instance of 'Role' EF requires that a parameterless constructor be declared.

When i change type:

RoleManager<IdentityRole> roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

Have this exception:

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]' has been registered.

How i can fix this problem and add useRoleManager for add role? Cheers!

Upvotes: 1

Views: 900

Answers (1)

Denis Baciu
Denis Baciu

Reputation: 169

You have to declare a constructor and members in class Role and link to RoleManager class if there is a relationship btw them. You can follow this tutorial and have it genereated automatically: Microsoft

Upvotes: 1

Related Questions