System.NotSupportedException: Store does not implement IUserRoleStore<TUser>.
was thrown by UserManager.AddToRoleAsync because Identity had been registered
without roles. Add .AddRoles<IdentityRole>() before
.AddEntityFrameworkStores<AppDb>(), and create the role before you add a user
to it.
AddIdentityCore<IdentityUser>() on its own gave a user store without role
support. The user was created without trouble; only the role call failed.
The error
Console output of a startup seed, the first line on standard output and the exception on the error stream:
Create user: Succeeded
Unhandled exception. System.NotSupportedException: Store does not implement IUserRoleStore<TUser>.
...
Why it happens
The registration in the broken version had no roles in it:
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>();
...
and the seed went on to users.AddToRoleAsync(admin, "Admin"). The message names
IUserRoleStore<TUser>, and the store this registration produced did not
implement it. The context was an
IdentityDbContext<IdentityUser> in both versions, so the database side was not
the problem; the registration was. The exception type is NotSupportedException,
which is a quick way to tell it apart from Role ADMIN does not exist., an
InvalidOperationException that the same call threw in the lab once roles were
registered and the role was missing.
The fix
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppDb>();
...
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
var roles = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
if (!await roles.RoleExistsAsync("Admin"))
Console.WriteLine($"Create role: {await roles.CreateAsync(new IdentityRole("Admin"))}");
var admin = new IdentityUser { UserName = "lena.fischer@example.test", Email = "lena.fischer@example.test" };
Console.WriteLine($"Create user: {await users.CreateAsync(admin, "Spring-2026x")}");
Console.WriteLine($"Add to role: {await users.AddToRoleAsync(admin, "Admin")}");
...
On a fresh database this printed Create role: Succeeded,
Create user: Succeeded and Add to role: Succeeded. The order of the
builder calls matters: with .AddEntityFrameworkStores<AppDb>() first and
.AddRoles<IdentityRole>() after it, the same seed failed at the
RoleManager line:
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore`1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]'.
...
The role step is there because
AddRoles alone is not enough for this seed: the next post down the same road is a
missing role, linked below.
If the app uses the full AddIdentity registration rather than
AddIdentityCore, the role type is its second type argument. In the same lab,
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDb>();
with no AddRoles line ran the same seed and printed the same three
Succeeded lines.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, a SQLite file created with
EnsureCreated(), and a seed in a scope after builder.Build() that creates
one fictional user and adds it to a role. The user, its .test address, AppDb
and lab.db are the test's; so is the password, and a real seed reads it from
configuration or user-secrets, never from source. Runs used: without AddRoles and no
role step (a new folder, no database file yet); with AddRoles before the stores and the
role step; with AddIdentity<IdentityUser, IdentityRole>(); and with
AddRoles after the stores. The later runs each moved the previous file aside first, and
one more run that ran a stale build is kept in the evidence with a note. Every run was built with
dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620 (Production).
Frequently asked
- Why does AddToRoleAsync say Store does not implement IUserRoleStore<TUser>?
- Identity was registered without roles. Add .AddRoles<IdentityRole>() before .AddEntityFrameworkStores<AppDb>() in the AddIdentityCore chain.
- Do I need AddRoles with AddIdentityCore?
- In the lab, yes: AddIdentityCore<IdentityUser>() alone created users but threw on AddToRoleAsync. With AddRoles<IdentityRole>() the role step succeeded.
- What comes after AddRoles to add a user to a role?
- Create the role with RoleManager<IdentityRole>.CreateAsync, guarded by RoleExistsAsync, then call AddToRoleAsync. Without the role the call throws Role ADMIN does not exist.
With roles registered but the role not yet created, the same call throws 'Role ADMIN does not exist'. More decoded errors in the Fixes category.