title: EntityType 'IdentityUserLogin' has no key defined. After trying to set up Foreign Key
body: <p>I am using entity framework code-first design.</p>
  
  <p>For my application a customer should have a creator which is an user from AspNetUser.</p>
  
  <p>Customer Class</p>
  
  <pre><code>    public class Customer
      {
          public string CreatorId { get; set; }
          [ForeignKey("CreatorId")]
          public ApplicationUser Creator { get; set; }
      }
  </code></pre>
  
  <p>IdentityModel</p>
  
  <pre><code>    public class ApplicationUser : IdentityUser
      {
          public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
          {
              // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
              // Add custom user claims here
              return userIdentity;
          }
      }
  
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public ApplicationDbContext()
              : base("AgueroCRMDbContext", throwIfV1Schema: false)
          {
          }
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  </code></pre>
  
  <p>After I build I can still register/login but I get this error when trying to access Customers</p>
  
  <pre><code>AgueroCRM.Data.Service.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
  AgueroCRM.Data.Service.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.
  
  Line 38:         public IEnumerable&lt;Customer&gt; GetAll()
  Line 39:         {
  Line 40:             return from r in db.Customer    &lt;-- error occurs here
  Line 41:                    orderby r.Id
  Line 42:                    select r;
  </code></pre>
  
  <p>Everything was working until I decided to add this foreign key.</p>
  
  <p>Edit: I think this might have something to do with not able to access the IdentityDbContext that contain ApplicationUser from the DbContext that contain Customers.</p>
  
  <p>Anyone know how to solve this?</p>
  
  <p><strong>Solved:</strong> by merging my ApplicationDbContext into my Main DbContext</p>
  
  <pre><code>    public class AgueroCRMDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public DbSet&lt;Customer&gt; Customer { get; set; }
  
          public AgueroCRMDbContext()
              : base("AgueroCRMDbContext", throwIfV1Schema: false)
          {
          }
  
          public static AgueroCRMDbContext Create()
          {
              return new AgueroCRMDbContext();
          }
      }
  </code></pre>