title: Entity Framework Code First override onModelCreating() for TPT Inheritance Screwing Up Identity User and Role Models
body: <p>I'm having problems extending the Fluent API to my inheritance classes.  I have taken the TPT (table per type) method and each type of the inheritance has a table.  I like table per type because the database is fully normalized and easy to maintain.  I am getting an error with overiding the <code>onModelCreating</code> method that this <a href="https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt" rel="nofollow noreferrer">article</a> instructed me to do.  It is losing the keys to the User and Roles Entities.</p>
  
  <p>Base Abstract Class </p>
  
  <pre><code>namespace Home_Warranty.Models
  {
      public abstract class Vendor
      {
          [Key]
          public int VendorID { get; set; }
  
          [Required]
          public string CompanyName { get; set; }
  
          [Required]
          public int StreetNumber { get; set; }
  
          [Required]
          public string StreetName { get; set; }
  
          }
  }
  </code></pre>
  
  <p>Inherited ServiceCompany Class from Vendor</p>
  
  <pre><code>namespace Home_Warranty.Models
   {
   [Table("ServiceCompanies")]
   public class ServiceCompany : Vendor
   {
      public string ACHClaim { get; set; }
  
      public virtual ICollection&lt;SubContractorCompany&gt; SubContractorCompanies{ get; set; }
  
      public virtual ICollection&lt;ServiceCompanyUser&gt; SubContractorUsers { get;set; }
      }
  }
  </code></pre>
  
  <p>Where I added the entity models to enable the Fluent API with <code>onModelCreating()</code></p>
  
  <pre><code>    public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false)
      {
      }
  
      public DbSet&lt;Vendor&gt; Vendors { get; set; }
  
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;ServiceCompany&gt;().ToTable("ServiceCompanies");
  
      }
  
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  }
  </code></pre>
  
  <p>I'm getting the following error on my migration while updating database.  It definitely because of me overiding onCreateModel function.  </p>
  
  <pre><code>at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
  One or more validation errors were detected during model generation:
  Home_Warranty.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
  Home_Warranty.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
  IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
  IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
  </code></pre>
  
  <ol>
  <li>This doesn't seem like the best way to approach the issue.  Can I add this code somewhere else and get the actual results I want without screwing up the Users and Roles models?  </li>
  <li>I would like to be able to do something like this with the fluent API.</li>
  </ol>
  
  <p><code>var ListofServiceCompanies = db.ServiceCompanies.All()</code></p>
  
  <p>or should I just go through the Vendor Entity instead?</p>
  
  <pre><code>var ListofServiceCompanies = db.Vendor.SelectMany( Vendor is a ServiceComapny...etc)
  </code></pre>
  
  <p>I prefer to set up the entities correctly and make the code nice and easy to use.  Any insight or knowledge is appreciated.</p>