2016-12-02 103 views
0

我正在使用MVC,ASP Identity和EntityFramework。EntityType'IdentityUserLogin'没有定义密钥/ EntityType'IdentityUserRole'没有定义密钥

我想让我的自定义ASP身份模型,并让它与一些自定义表交互,我想扩大原来的。

我不断得到相同的错误,一直在尝试解决这个问题整整两天。

的错误如下:

One or more validation errors were detected during model generation: 

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

我已经尝试了所有的解决方案,我能找到的其他问题都没有用,这是慢慢地变成了一场噩梦。

这是我的代码; 实体:IdentityDbContext

public class Entities : IdentityDbContext<ApplicationUsers, Role, 
    int, UserLogin, ApplicationUserRole, UserClaim> 
    { 
     public Entities() 
      : base("name=Entities") 
     { 
      Database.SetInitializer<Entities>(null); 
      Configuration.LazyLoadingEnabled = false; 
      Configuration.ProxyCreationEnabled = false; 
     } 

     public static Entities Create() 
     { 
      return new Entities(); 
     } 

     //public virtual DbSet<ApplicationUsers> ApplicationUsers { get; set; } 
     public virtual DbSet<Finding> Findings { get; set; } 
     public virtual DbSet<Project> Projects { get; set; } 
     //public virtual DbSet<Role> Roles { get; set; } 
     public virtual DbSet<ApplicationUserProject> ApplicationUserProjects { get; set; } 
     public virtual DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; } 
     public virtual DbSet<UserClaim> UserClaims { get; set; } 
     public virtual DbSet<UserLogin> UserLogins { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 

      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

      modelBuilder.Entity<IdentityUser>().Ignore(c => c.PhoneNumber) 
              .Ignore(c => c.PhoneNumberConfirmed) 
              .Ignore(c => c.TwoFactorEnabled) 
              .Ignore(c => c.LockoutEndDateUtc) 
              .Ignore(c => c.LockoutEnabled) 
              .Ignore(c => c.AccessFailedCount) 
              .Ignore(c => c.UserName); 

      modelBuilder.Entity<IdentityUser>().ToTable("ApplicationUser", "dbo"); 
      modelBuilder.Entity<UserClaim>().ToTable("UserClaim", "dbo"); 
      modelBuilder.Entity<Role>().HasKey<int>(l => l.Id).ToTable("Role", "dbo"); 

      modelBuilder.Entity<UserLogin>().HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId }) 
                .ToTable("UserLogin", "dbo"); 
      modelBuilder.Entity<ApplicationUserRole>().HasKey(l => new { l.ApplicationUserId, l.RoleId }) 
               .ToTable("ApplicationUserRole", "dbo"); 

      modelBuilder.Entity<ApplicationUsers>().Property(r => r.Id); 
      modelBuilder.Entity<UserClaim>().Property(r => r.Id); 
      modelBuilder.Entity<UserLogin>().Property(r => r.UserId); 
      modelBuilder.Entity<Role>().Property(r => r.Id); 
     } 
    } 

的ApplicationUserRole:IdentityUserRole

public class ApplicationUserRole : IdentityUserRole<int> 
    { 
     public int ApplicationUserId { get; set; } 
     public override int RoleId { get; set; } 
     public System.DateTime DateCreated { get; set; } 
     public System.DateTime DateChanged { get; set; } 
     public int CreateUser { get; set; } 
     public int ChangeUser { get; set; } 

     public virtual ApplicationUsers ApplicationUser { get; set; } 
     public virtual Role Role { get; set; } 
    } 

的用户登陆:IdentityUserLogin

public class UserLogin : IdentityUserLogin<int> 
{ 
    public string LoginProvider { get; set; } 
    public string ProviderKey { get; set; } 
    public int ApplicationUserId { get; set; } 

    public virtual ApplicationUsers ApplicationUser { get; set; } 
} 

我希望这是足够的信息,任何线索,为什么这些错误滚滚而来?

回答

1

您需要定义新的自定义表的主键,例如:

[Key] 
public int UserLoginId { get; set; } 

如果你需要一个复合键与您现有的栏目,然后使用:

[Key, Column(Order = 0)] 
public string LoginProvider { get; set; } 

[Key, Column(Order = 1)] 
public string ProviderKey { get; set; } 

[Key, Column(Order = 2)] 
public int ApplicationUserId { get; set; } 
相关问题