title: ASP.NET Identity with EF Database First MVC5
body: <p>Is it possible to use the new Asp.net Identity with Database First and EDMX? Or only with code first?</p>
  
  <p>Here's what I did:</p>
  
  <p>1) I made a new MVC5 Project and had the new Identity create the new User and Roles tables in my database.</p>
  
  <p>2) I then opened my Database First EDMX file and dragged in the new Identity Users table since I have other tables that relate to it.</p>
  
  <p>3) Upon saving the EDMX, the Database First POCO generator will auto create a User class. However, UserManager and RoleManager expects a User class inheriting from the new Identity namespace (Microsoft.AspNet.Identity.IUser), so using the POCO User class won't work.</p>
  
  <p>I guess a possible solution is to edit my POCO Generation Classes to have my User class inherit from IUser?</p>
  
  <p>Or is ASP.NET Identity only compatible with Code First Design?</p>
  
  <p>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p>Update:  Following Anders Abel's suggestion below, this is what I did. It work's, but I'm wondering if there is a more elegant solution.</p>
  
  <p>1) I extended my entity User class by creating a partial class within the same namespace as my auto generated entities.</p>
  
  <pre><code>namespace MVC5.DBFirst.Entity
  {
      public partial class AspNetUser : IdentityUser
      {
      }
  }
  </code></pre>
  
  <p>2) I changed my DataContext to inherit from IdentityDBContext instead of DBContext. Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.</p>
  
  <pre><code> public partial class MVC5Test_DBEntities : IdentityDbContext&lt;AspNetUser&gt;  //DbContext
  </code></pre>
  
  <p>3) Within your auto generated User entity class, you must add the override keyword to the following 4 fields or comment these fields out since they are inherited from IdentityUser (Step 1).  Note that every time you update your EDMX and regenerate the DBContext and Entity classes, you'll have to set this back to this.</p>
  
  <pre><code>    override public string Id { get; set; }
      override public string UserName { get; set; }
      override public string PasswordHash { get; set; }
      override public string SecurityStamp { get; set; }
  </code></pre>