title: How to list users with role names in ASP.NET MVC 5
body: <p>I have default project template of ASP.NET MVC 5 web site and I am trying to list all users with role names (not IDs).</p>
  
  <p>The query is:</p>
  
  <pre><code>db.Users.Include(u =&gt; u.Roles).ToList()
  </code></pre>
  
  <p>Then I want to print the role names with something like:</p>
  
  <pre><code>@string.Join(", ", user.Roles.Select(r =&gt; r.RoleId))
  </code></pre>
  
  <p>The problem is that I can reach only <code>RoleId</code>, not Role class where <code>Name</code> and other properties are stored.</p>
  
  <p>I could run another select to get all roles and then use that as a lookup. Or write a join in the query directly? I am not sure how because I do not have access to the table with <code>IdentityUserRole</code> entities that are binding users and roles together.</p>
  
  <p>The root of the problem seems to be the fact that is Roles collection of <code>IdentityUserRole</code> (not <code>Role</code>) which contains only <code>RoleId</code> and <code>UserId</code>.</p>
  
  <pre><code>public class IdentityUserRole&lt;TKey&gt; {
      public virtual TKey RoleId { get; set; }
      public virtual TKey UserId { get; set; }
  }
  </code></pre>
  
  <p>I thought that if one want to do N-to-N relationship in EF they should put directly collection of <code>Roles</code> and then override <code>OnModelCreating</code> and specify the relationships. This approach seems to complicate browsing the objects from one to another.</p>
  
  <p>Why they decided to include <code>IdentityUserRole</code> as extra entity? To be able to add extra data to the relationships? At cost of not being able to navigate from users to roles?</p>