2016-12-02 2 views
0

Я использую MVC, ASP Identity и EntityFramework.EntityType 'IdentityUserLogin' не имеет определенного ключа/EntityType 'IdentityUserRole' не имеет определенного ключа

Я пытаюсь настроить свои персональные модели ASP Identity и взаимодействовать с некоторыми пользовательскими таблицами, я хочу расширить оригинал.

Я продолжаю получать одну и ту же ошибку снова и снова, пытаюсь исправить это в течение 2 полных дней.

Ошибка заключается в следующем:

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; } 
    } 

UserLogin: 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; } 
Смежные вопросы