EntityType' IdentityUserLogin'没有定义键

时间:2018-04-02 17:06:15

标签: c# asp.net asp.net-mvc asp.net-identity ef-migrations

我正在尝试在我的应用程序中使用个人用户帐户。由于我正在为与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>();
        }

    }

我已经将我的IdentityModel类拆分了,所以DbContext自己就在它的类中。我的IdentityModel类看起来像这样:

public class ApplicationUser : IdentityUser
{

    public Boolean Available { get; set; }
    public Boolean ActivelySeeking { get; set; }
    public Boolean AcceptedUseOfData { get; set; }
    public int ExpectedHourlySalary { get; set;}
    public Boolean Gender { get; set;}
    public DateTime? DateOfBirth { get; set; }
    public String FirstName { get; set; }
    public string LastName { get; set; }

    public int JobExperienceId { get; set; }
    [ForeignKey("JobExperienceId")]
    public virtual DbSet<JobExperience> JobExperiences { get; set; }

    public int LanguageId { get; set; }
    [ForeignKey("LanguageId")]
    public virtual DbSet<Language> languages { get; set; }

    public int CertificateId { get; set; }
    [ForeignKey("CertificateId")]
    public virtual DbSet<Certificate> certificates { get; set; }


    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

我正在尝试进行迁移的证书类可以在这里看到:

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

1 个答案:

答案 0 :(得分:0)

我找到了答案!

我将OnModelCreating方法置于IdentityDbContext类中,从而错误放置了它。相反,与IdentityUser类相关的每个类都有自己的DbContext类,并且所有DbContext类都必须调用下面显示的方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        }