0

I tried to add ASP.NET identity to my existing MVC 5 project. I applied some articles about this issue.

I followed this articles: Adding ASP.NET MVC5 Identity Authentication to an existing project http://www.yagizhanpala.com/asp_net_identity_ile_mvc_projede_uyelik_islemleri/

But after the implementation, I had same problem.

my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
my.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.

Note:I am using reverse code engineer to create model class.

public class ApplicationRole : IdentityRole 
{ 
    public string Description { get; set; } 
    public ApplicationRole() { } 
    public ApplicationRole(string roleName, string description) : base(roleName) 
    { 
        this.Description = description; 
    } 
}

public class ApplicationUser : IdentityUser 
{ 
    public string Name { get; set; } 
    public string Surname { get; set; } 
}

OnModelcreating() method includes:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Configurations.Add(new LoginMap());
    modelBuilder.Configurations.Add(new RegisterMap()); 
} 
Community
  • 1
  • 1
H.C
  • 13
  • 4
  • It sounds like your identity models are not inheriting from the correct base classes (e.g. from the linked question: `public class AppRole : IdentityRole { ... }`). – Brendan Green Jan 20 '16 at 20:34
  • Could you please submit your `User` and `Role` classes code? – Sam FarajpourGhamari Jan 21 '16 at 07:45
  • **public class ApplicationRole : IdentityRole { public string Description { get; set; } public ApplicationRole() { } public ApplicationRole(string roleName, string description) : base(roleName) { this.Description = description; } }** – H.C Jan 21 '16 at 10:33
  • _ public class ApplicationUser : IdentityUser { public string Name { get; set; } public string Surname { get; set; } }_ – H.C Jan 21 '16 at 10:35
  • And I tried another articles.Its Appuser and AppRole code: public class Appuser:IdentityUser { public string Name { get; set; } public string Surname { get; set; } public string MyExtraProperty { get; set; } } – H.C Jan 21 '16 at 10:38
  • public class AppRole:IdentityRole { public AppRole() : base() { } public AppRole(string name) : base(name) { } } – H.C Jan 21 '16 at 10:40
  • I think, you have overridden your `DbContext`'s `OnModelCreating` method. Am I right? – Sam FarajpourGhamari Jan 21 '16 at 10:52
  • Yes, you are right. I think to. onmodelcreating() method includes: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new LoginMap()); modelBuilder.Configurations.Add(new RegisterMap()); } – H.C Jan 21 '16 at 11:34

1 Answers1

1

You must include IdentityDbContext's configurations in your customized DBContext too. Simply call the parent's method in your overridden method like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    base.OnModelCreating(modelBuilder); 
    modelBuilder.Configurations.Add(new LoginMap());
    modelBuilder.Configurations.Add(new RegisterMap()); 
}
Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56
  • I tried it but not worked. I had this error...An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code – H.C Jan 21 '16 at 12:02
  • What is the exception's message? – Sam FarajpourGhamari Jan 21 '16 at 12:03
  • The entity type ApplicationUser is not part of the model for the current context. – H.C Jan 21 '16 at 14:36
  • Are you sure your DbContext is inherited from `IdentityDbContext` **not** from `DbContext` itself? – Sam FarajpourGhamari Jan 21 '16 at 14:41
  • now, I had this error..................An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in mscorlib.dll but was not handled in user code Additional information: An error occurred while executing the command definition. See the inner exception for details.....Could you suggest me another article or video for this issue – H.C Jan 21 '16 at 14:49
  • Your mentioned posts are fine. You probably missing something. What is the inner exception's message? – Sam FarajpourGhamari Jan 21 '16 at 14:55