我在VS2015中创建了一个应用程序(使用Identity 2.2)和示例应用程序(MVC,mvc个人身份验证),我根据教程修改了一些自定义字段添加到users表(使用迁移来获取添加的字段)进入数据库)。 然后对于其余的表(非标识的)我使用EntityFramework数据库 - 首先生成CRUD控制器&观点等 到目前为止一切顺利,一切似乎都很好。
现在我正在尝试修改它,以便Identity表的键是整数而不是GUID。我已经按照本教程(使用MVC更新3), https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#context
但是在完成教程之后,当我构建并运行解决方案时,我在尝试生成模型时遇到此错误
One or more validation errors were detected during model generation:
Proyecto.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
我不知道如何修复它...感谢任何帮助。
my Models / IdentityModels.cs:
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
//added custom fields
[Column(TypeName = "varchar")]
[StringLength(50)]
public string Nombre { get; set; }
public DateTime FechaAlta { get; set; }
public DateTime? FechaUltModif { get; set; }
public bool Activo { get; set; }
public string Notas { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//to fix some conflicts using mysql
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255);
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
}
//aded for int keys
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
public CustomRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
答案 0 :(得分:1)
以下是您的问题:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//your problem is in this line change IdentityRole to CustomRole
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
使用CustomRole
代替IdentityRole
,因为IdentityRole
使用字符串主键定义,而CustomUserRole
使用int
< / p>
如果你去定义IdentityRole
,你会发现它的扩展IdentityRole<string, IdentityUserRole>
以及你收到错误的原因
Currected Code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//to fix some conflicts using mysql
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255);
modelBuilder.Entity<CustomRole>().Property(r => r.Name).HasMaxLength(255);
}