0

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

4
  • Do you have a custom UserManager implementation? Commented Jul 14, 2017 at 12:53
  • Or does you database perhaps have some bad data in it? Commented Jul 14, 2017 at 12:53
  • There were duplicated rows in my IdentityRoles table. Removing these solved the problem. Thank you for your help! Commented Jul 14, 2017 at 12:59
  • 2
    No problem, but next time wait for the person who suggested the possibility to add the answer so they can get the recognition :) Commented Jul 14, 2017 at 13:02

1 Answer 1

2

There are duplicated names in the IdentityRoles table:

SELECT *
FROM [dbo].[IdentityRoles]

enter image description here

Deleting the unnecessary rows solved the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Something went wrong in your database. Make sure that Name column has a UNIQUE CONSTRAINT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.