title: Querying EF Core identity roles
body: <p>I'm using .NET Core 3.1 and I want to query the users in my database.</p>
  <p>So I modified my <code>ApplicationUser</code> class as follows:</p>
  <pre><code>public class ApplicationUser : IdentityUser
  {
      public ICollection&lt;IdentityUserRole&lt;string&gt;&gt; UserRoles { get; set; }
  }
  </code></pre>
  <p>And now I'm trying to query the Users.</p>
  <pre><code>var users = from u in DbContext.Users
              select new UserViewModel
              {
                  Email = u.Email,
                  EmailConfirmed = u.EmailConfirmed,
                  Phone = u.PhoneNumber,
                  Roles = u.UserRoles.Select(r =&gt; r.Role.Name) // Whoops! Won't work
              };
  </code></pre>
  <p>The problem is that <code>UserRoles</code> have a <code>RoleId</code> property, but they don't have a <code>Role</code> property.</p>
  <p>So how would I get the name of those roles?</p>
  <p><strong>Update:</strong></p>
  <p>Based on Jawad's comments, I changed my code as follows:</p>
  <pre><code>public class ApplicationUser : IdentityUser
  {
      public ICollection&lt;IdentityRole&gt; Roles { get; set; }
  }
  </code></pre>
  <p>And</p>
  <pre><code>Users = (from u in DbContext.Users
           select new UserViewModel
           {
               Email = u.Email,
               EmailConfirmed = u.EmailConfirmed,
               Phone = u.PhoneNumber,
               Roles = u.Roles.Select(r =&gt; r.Name)
           })
           .ToList();
  </code></pre>
  <p>I was thinking I couldn't do that because it's a many-to-many relationship. But in fact, it does compile. However, when I run it I get an error.</p>
  <blockquote>
  <p>Invalid column name 'ApplicationUserId'.</p>
  </blockquote>
  <p>I'm not sure where that column name is being referenced. It doesn't appear to be a column name in the database.</p>
  <p><strong>Update 2:</strong></p>
  <p>Turns out, the error above is because a database migration was pending. However, I didn't want to change the database. As I initially suspected, I can't do <code>ApplicationUser.ICollection&lt;IdentityRole&gt;</code> because there is an intermediate table.</p>
  <p>So I got this to run, but it returned no results (because there are no foreign keys from <code>IdentityRole</code> to <code>ApplicationUser</code>.</p>
  <p>So, the question stands: how can I query users along with their roles?</p>