title: Asp.Net Identity - IdentityDbContext<TUser> causing problems for fields added to AspNetRoles table
body: <h2>IdentityDbContext causing problems for properties/fields added to AspNetRoles</h2>
  
  <p>I believe the problem is here with IdentityDbContext taking an IdentityUser type:</p>
  
  <pre><code>public class MyIdentityDb : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public IdentityDb()
          : base("IdentityDb")
      {
      }
  }
  </code></pre>
  
  <p><strong>But let me explain…</strong></p>
  
  <p>It is wonderful that we can add fields to the AspNetUsers table by adding properties to our ApplicationUser class that inherits from IdentityUser. Example:</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
      {
          [Required]
          [StringLength(50)]
          public string FirstName { get; set; }
  
          [Required]
          [StringLength(50)]
          public string LastName { get; set; }
      }
  </code></pre>
  
  <p>So natural we can add fields to the AspNetRoles table by adding properties to an ApplicationRole class that inherits from IdentityRole. Example:</p>
  
  <pre><code>public class ApplicationRole : IdentityRole
  {
      [Required]
      [StringLength(50)]
      public string ProperName { get; set; }
  }
  </code></pre>
  
  <p>Works perfect. We can see the field in the database. We can add data to it. Example:</p>
  
  <pre><code>RoleManager&lt;ApplicationRole&gt; roleManager = new RoleManager&lt;ApplicationRole&gt;(new RoleStore&lt;ApplicationRole&gt;(new MyIdentityDb()));
  
  var role = new ApplicationRole() { Name = name, ProperName = propername };
  var result = await roleManager.CreateAsync(role);
  </code></pre>
  
  <p>But now we run into a problem when trying to get to the data. Example:</p>
  
  <p>Our ViewModel:</p>
  
  <pre><code>public class IndexViewModel
  {
      public IList&lt;ApplicationUser&gt; Users { get; set; }
      public IList&lt;ApplicationRole&gt; Roles { get; set; }
  }
  </code></pre>
  
  <p>On our Controller:</p>
  
  <pre><code>private MyIdentityDb myIdentityDb = new MyIdentityDb();
  </code></pre>
  
  <p>Our Index method on our controller:</p>
  
  <pre><code>public ViewResult Index(int? page)
  {
       return View(new IndexViewModel
       {
            Users = myIdentityDb.Users.ToList(),
            Roles = myIdentityDb.Roles.ToList()
       });
  }
  </code></pre>
  
  <p><strong>The error is on “myIdentityDb.Roles.ToList()” and reads “Cannot implicitly convert type <code>System.Collection.Generic.List&lt;Microsoft.AspNet.Identity.EntityFramework.IdentityRole&gt; to System.Collections.Generic.IList&lt;MyApp.Models.ApplicationRole&gt;</code>…</strong></p>
  
  <p>Of course we could change our ViewModel to use type IdentityRole like the following example, but then we cannot get to the new “ProperName” field in the AspNetRoles table:</p>
  
  <pre><code>public class IndexViewModel
  {
      public IList&lt;ApplicationUser&gt; Users { get; set; }
      public IList&lt;IdentityRole&gt; Roles { get; set; }
  }
  </code></pre>
  
  <p>So we could try creating another Db class and pass it an IdentityRole type instead of IdentityUser:</p>
  
  <pre><code>public class MyIdentityDb : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public MyIdentityDb()
          : base("MyIdentityDb")
      {
      }
  }
  
  public class MyIdentityRolesDb : IdentityDbContext&lt;ApplicationRole&gt;
  {
      public MyIdentityRolesDb()
          : base("MyIdentityDb")
      {
      }
  }
  </code></pre>
  
  <p>And change our controller:</p>
  
  <pre><code>private MyIdentityDb myIdentityDb = new MyIdentityDb();
  private MyIdentityRolesDb myIdentityRolesDb = new MyIdentityRolesDb();
  </code></pre>
  
  <p>And change our Index method on our controller:</p>
  
  <pre><code>public ViewResult Index(int? page)
  {
       return View(new IndexViewModel
       {
            Users = myIdentityDb.Users.ToList(),
            Roles = myIdentityRolesDb.Roles.ToList()
       });
  }
  </code></pre>
  
  <p>But we end up with the same problem; the fact that IdentityDbContext takes an IdentityUser type.</p>
  
  <p><em><strong>Any ideas how we can get a list of Roles with the custom fields/properties?</em></strong></p>