Liladhar
Liladhar

Reputation: 383

How to get User Role for each user in Data access layer in Identity (MVC5)?

Application is of MVC5 Identity based in c#. As observed, AspNetUserRoles is not populated in EDMX. Certainly cannot query on it using LINQ.

In Data access layer, having the function GetAllUsers(). For each user need to get its role which is mapped in AspNetUserRoles table. Able to get all users but not having its role. Hot to get role for each user ?

Upvotes: 2

Views: 1281

Answers (1)

Kien Chu
Kien Chu

Reputation: 4895

AspNetUserRoles is a mapping table (many-to-many), so it's not generated in EDMX (by design, it does not have primary key)

You can get roles of user by using navigation properties:

user.Include(u => u.Roles); // only need if lazy loading disabled
var roles = user.Roles; 

Upvotes: 3

Related Questions