Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityType“IdentityUserLogin”未定义密钥_C#_Asp.net_Asp.net Mvc_Asp.net Identity_Entity Framework Migrations - Fatal编程技术网

C# EntityType“IdentityUserLogin”未定义密钥

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

我正在尝试在我的应用程序中使用个人用户帐户。当我为任何与ApplicationUser类相关的类而不是ApplicationUser本身进行迁移时,我得到的错误是IdentityUserLogin、IdentityUserRole等没有定义键

正如其他答案中所提到的,我已将调用添加到OnModelCreating,但没有任何运气:

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