title: Integrating ASP.NET Identity into Existing DbContext
body: <p>I'm working on an ASP.NET MVC 5 project in VS2013, .NET 4.5.1, that uses Entity Framework 6 Code-First. I have a decent size database built out and somewhat working (project is about two weeks old). I want to integrate user authentication now, but I'm not sure how to approach it. After spending most of the day researching I've decided to give the new ASP.NET Identity framework a shot over having to write custom Membership or Role providers. What I'm confused about is how to make it all work with the existing database/model I have.</p>
  
  <p>Currently I have an object called <code>Employee</code> that holds basic employee information (for now). After having pondered the question all day, I decided to decouple authentication from it into a <code>User</code> object, which is what Identity wants anyway. That being said how do I make it all work?</p>
  
  <p>Here's my <code>Employee</code> class:</p>
  
  <pre><code>public class Employee : Person {
      public int EmployeeId { get; set; }
      public byte CompanyId { get; set; }
      public string Name {
          get {
              return String.Format("{0} {1}", this.FirstName, this.LastName);
          }
      }
      public string Password { get; set; }
      public bool IsActive { get; set; }
  
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
      public virtual Company Company { get; set; }
      public virtual ICollection&lt;Email&gt; Emails { get; set; }
      public virtual ICollection&lt;Phone&gt; Phones { get; set; }
  
      public Employee() {
          this.Addresses = new List&lt;Address&gt;();
          this.Emails = new List&lt;Email&gt;();
          this.Phones = new List&lt;Phone&gt;();
      }
  }
  </code></pre>
  
  <p>And my <code>DbContext</code> derived class:</p>
  
  <pre><code>public class DatabaseContext : DbContext {
      static DatabaseContext() {
          Database.SetInitializer&lt;DatabaseContext&gt;(new DatabaseInitializer());
      }
  
      public DatabaseContext()
          : base("Name=DatabaseContext") {
          this.Database.Initialize(true);
      }
  
      public DatabaseContext(
          string connectionString)
          : base(connectionString) {
          this.Database.Initialize(true);
      }
  
      /// DbSets...
  
      public override int SaveChanges() {
          try {
              return base.SaveChanges();
          } catch (DbEntityValidationException e) {
              IEnumerable&lt;string&gt; errors = e.EntityValidationErrors.SelectMany(
                  x =&gt;
                      x.ValidationErrors).Select(
                  x =&gt;
                      String.Format("{0}: {1}", x.PropertyName, x.ErrorMessage));
  
              throw new DbEntityValidationException(String.Join("; ", errors), e.EntityValidationErrors);
          }
      }
  
      protected override void OnModelCreating(
          DbModelBuilder modelBuilder) {
          modelBuilder.Ignore&lt;Coordinate&gt;();
  
          /// Configs...
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>