my.Models.IdentityUserLogin :: EntityType'IdentityUserLogin'没有定义键

时间:2016-01-20 20:09:17

标签: asp.net-mvc asp.net-identity

我尝试将ASP.NET身份添加到我现有的MVC 5项目中。我申请了一些关于这个问题的文章。

我遵循了这篇文章: Adding ASP.NET MVC5 Identity Authentication to an existing project http://www.yagizhanpala.com/asp_net_identity_ile_mvc_projede_uyelik_islemleri/

但是在实施之后,我遇到了同样的问题。

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.

注意:我正在使用反向代码工程师来创建模型类。

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()方法包括:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Configurations.Add(new LoginMap());
    modelBuilder.Configurations.Add(new RegisterMap()); 
} 

1 个答案:

答案 0 :(得分:1)

您还必须在自定义的DBContext中包含IdentityDbContext个配置。只需在覆盖的方法中调用父方法,如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    base.OnModelCreating(modelBuilder); 
    modelBuilder.Configurations.Add(new LoginMap());
    modelBuilder.Configurations.Add(new RegisterMap()); 
}