2016-12-02 102 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; } 
相關問題