System.InvalidOperationException: Role ADMIN does not exist. was thrown by
UserManager.AddToRoleAsync(user, "Admin") because no role called Admin had been
created yet. Create it with RoleManager<IdentityRole> before you add users
to it.
The upper-case ADMIN in the message is the normalized role name; the code passed
"Admin". Seed roles first, guarded by RoleExistsAsync, then users, then
the role memberships.
The error
Console output of a startup seed: the first line on standard output, the exception on the error stream:
Create user: Succeeded
Unhandled exception. System.InvalidOperationException: Role ADMIN does not exist.
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`10.AddToRoleAsync(TUser user, String normalizedRoleName, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.UserManager`1.AddToRoleCoreAsync(TUser user, String role)
at Microsoft.AspNetCore.Identity.UserManager`1.AddToRoleAsync(TUser user, String role)
...
Why it happens
Identity was registered with roles (AddRoles), but no role had been created in the
database. The seed went straight from
creating the user to adding the role:
...
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")}");
...
The stack trace names the parameter normalizedRoleName: the store looked the role up
by its normalized name and found nothing. Unlike CreateAsync, which returned an
IdentityResult a line earlier, this call threw, and the exception ended the app
before it started listening.
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 fixed
seed ran once; the RoleExistsAsync check is there so that a later start would skip
the role, which this lab did not run. For a seed that creates the same user on every start, see
the post linked below. The .AddRoles<IdentityRole>()
line was in both versions. Without it the same call fails earlier with a different message,
Store does not implement IUserRoleStore<TUser>.
Keep the order in one place: roles, then users, then memberships. If roles come from
configuration or a list in code, loop over that list with the same
RoleExistsAsync check before any user is touched, and print the
IdentityResult of each CreateAsync, as the lab does for the role. A seed that prints each step, as this one does, shows at a glance which line of the
sequence failed.
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, Identity registered with
AddIdentityCore<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<AppDb>(),
and a seed in a scope after builder.Build(), run once without the role step (in a new
project folder with no database file) and once with it (the previous file moved aside first).
Both were built with dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620; the fixed run printed
Hosting environment: Production. 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.
Frequently asked
- Why does AddToRoleAsync say Role ADMIN does not exist?
- No role with that name was created before the call. Create it with RoleManager<IdentityRole>.CreateAsync(new IdentityRole("Admin")), guarded by RoleExistsAsync, then call AddToRoleAsync.
- Why is the role name in capitals in the error?
- The message shows the normalized role name. The lab passed "Admin" and the exception said ADMIN; the store looked it up by that normalized name.
- Does AddToRoleAsync return an IdentityResult for a missing role?
- No, in the lab it threw System.InvalidOperationException, which ended the app because the seed ran at startup.
A seed that creates the user twice gets 'Username is already taken' instead. More decoded errors in the Fixes category.