{"record":{"id":"32713589f961ebb3","repo":"OrchardCMS/OrchardCore","slug":"role-normalizedrolename-does-not-exist","errorCode":null,"errorMessage":"Role {normalizedRoleName} does not exist.","messagePattern":"Role (.+?) does not exist\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Users.Core/Services/UserStore.cs","lineNumber":411,"sourceCode":"        return Task.CompletedTask;\n    }\n\n    #endregion IUserEmailStore<IUser>\n\n    #region IUserRoleStore<IUser>\n\n    public async Task AddToRoleAsync(IUser user, string normalizedRoleName, CancellationToken cancellationToken)\n    {\n        ArgumentNullException.ThrowIfNull(user);\n\n        if (user is User u)\n        {\n            var roleNames = await _roleService.GetRoleNamesAsync();\n\n            var roleName = roleNames.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName);\n            if (string.IsNullOrEmpty(roleName))\n            {\n                throw new InvalidOperationException($\"Role {normalizedRoleName} does not exist.\");\n            }\n\n            u.RoleNames.Add(roleName);\n        }\n    }\n\n    public async Task RemoveFromRoleAsync(IUser user, string normalizedRoleName, CancellationToken cancellationToken)\n    {\n        ArgumentNullException.ThrowIfNull(user);\n\n        if (user is User u)\n        {\n            var roleNames = await _roleService.GetRoleNamesAsync();\n\n            var roleName = roleNames.FirstOrDefault(r => NormalizeKey(r) == normalizedRoleName);\n            if (string.IsNullOrEmpty(roleName))\n            {\n                throw new InvalidOperationException($\"Role {normalizedRoleName} does not exist.\");","sourceCodeStart":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Users.Core/Services/UserStore.cs#L393-L429","documentation":"Thrown by UserStore.AddToRoleAsync when the requested normalized role name does not match any role returned by IRoleService.GetRoleNamesAsync(). The store refuses to attach a role that does not exist in the system, so the user is not modified.","triggerScenarios":"Calling UserManager.AddToRoleAsync(user, roleName) with a role name that is misspelled, has wrong casing after normalization, or was never created.","commonSituations":"Seeding users before roles are created in a recipe/migration; renaming a role while code still references the old name; case-sensitivity mismatches between code and the role definition.","solutions":["Create the role first (via IRoleService or RoleManager) before assigning it to users.","Verify the exact role name and its normalization (NormalizeKey uses culture-invariant lowercase); pass the same normalized value users see.","Check for typos or renamed roles and update the calling code or seed data.","Wrap in try-catch for InvalidOperationException to fail gracefully with a clear message."],"exampleFix":"// before\nawait _userManager.AddToRoleAsync(user, \"Adimn\");\n// after\nif (!await _roleManager.RoleExistsAsync(\"Admin\")) { await _roleManager.CreateAsync(new Role { RoleName = \"Admin\" }); }\nawait _userManager.AddToRoleAsync(user, \"Admin\");","handlingStrategy":"validation","validationCode":"var roleNames = await _roleService.GetRoleNamesAsync();\nbool exists = roleNames.Any(r => string.Equals(r, roleName, StringComparison.OrdinalIgnoreCase));","typeGuard":"bool RoleExists(IEnumerable<string> roles, string name) => roles?.Any(r => string.Equals(r, name, StringComparison.OrdinalIgnoreCase)) == true;","tryCatchPattern":"try { await _userManager.AddToRoleAsync(user, roleName); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not exist\"))\n{ _logger.LogWarning(\"Role {Role} missing\", roleName); }","preventionTips":["Create all roles before assigning users (recipe order: roles step before users step)","Normalize role names consistently (invariant lowercase)","Centralize role-name constants instead of inline strings","Validate role names when importing/migrating users"],"tags":["users","roles","identity"],"backgroundTag":"entity-not-found","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}