0

The error in package manager console :

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

Code I am using:

public string UserId { get; set; }
[ForeignKey("UserId")] 
public virtual ApplicationUser ApplicationUser { get; set; }

In OnmodelCreating I have:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}

I also checked the database, and the Identity tables have been created and have primary keys also.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Gaurav Mithas
  • 67
  • 2
  • 8
  • I think you're missing the primary key of the table. Try to add [Key] to your primary property in model. – Shawn Yan Jun 08 '16 at 02:50
  • What's the full model here? And why are you re-defining the keys for `IdentityUserLogin` and the other classes? Shouldn't you have a `base.OnModelCreating(modelBuilder)` call at the end of your override (presuming that this is your context, and you've inherited from `IdentityDbContext`)? – Brendan Green Jun 08 '16 at 04:26
  • See http://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit – Steve Greene Jun 08 '16 at 13:20

0 Answers0