Search code examples
c#asp.netentity-framework-6asp.net-identity.net-4.5

Asp.NET Identity Core IsInRole InvalidOperationException: Sequence contains more than one element


I have this code:

...
var _context = new MyDbContext();     
var _userManager = new UserManager<User>(new UserStore<User>(_context));
...
public bool IsInRole(string userId, string roleName)
{
    return _userManager.IsInRole(userId, roleName); //InvalidOperationException
}

Throws this exception:

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Sequence contains more than one element

Visual Studio 2015

.NET 4.5

Microsoft AspNet Identity Core 2.2.1

Entity Framework 6

InvalidOperationException


Solution

  • There are duplicated names in the IdentityRoles table:

    SELECT *
    FROM [dbo].[IdentityRoles]
    

    enter image description here

    Deleting the unnecessary rows solved the problem.