我使用的是MVC,ASP Identity和EntityFramework。
我正在尝试制作自定义的ASP身份模型并让它与某些自定义表进行交互,我想扩展原始版本。
我一遍又一遍地得到同样的错误,现在已经尝试解决这个错误了整整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; }
}
我希望这是足够的信息,为什么这些错误不断出现的任何线索?
答案 0 :(得分: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; }