C# EntityType“IdentityUserLogin”未定义密钥
我正在尝试在我的应用程序中使用个人用户帐户。当我为任何与ApplicationUser类相关的类而不是ApplicationUser本身进行迁移时,我得到的错误是IdentityUserLogin、IdentityUserRole等没有定义键 正如其他答案中所提到的,我已将调用添加到OnModelCreating,但没有任何运气:C# EntityType“IdentityUserLogin”未定义密钥,c#,asp.net,asp.net-mvc,asp.net-identity,entity-framework-migrations,C#,Asp.net,Asp.net Mvc,Asp.net Identity,Entity Framework Migrations,我正在尝试在我的应用程序中使用个人用户帐户。当我为任何与ApplicationUser类相关的类而不是ApplicationUser本身进行迁移时,我得到的错误是IdentityUserLogin、IdentityUserRole等没有定义键 正如其他答案中所提到的,我已将调用添加到OnModelCreating,但没有任何运气: public class IdentityDb : IdentityDbContext<ApplicationUser> { pu
public class IdentityDb : IdentityDbContext<ApplicationUser>
{
public IdentityDb() : base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
}
我找到了答案
我将OnModelCreating方法放错了位置,将其放在IdentityDbContext类中。相反,与IdentityUser类相关的每个类都有自己的DbContext类,所有DbContext类都必须调用如下所示的方法:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
public class Certificate
{
public string CertificateId { get; set; }
[Display(Name = "Certification-date")]
public DateTime? DateOfCertification { get; set; }
public string Name { get; set; }
[Display(Name = "Name of certifier")]
public string CertificationProvider { get; set;}
public virtual ApplicationUser ApplicationUser { get; set; }
public int ApplicationUserId { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}