Search code examples
c#asp.netasp.net-mvcasp.net-identityentity-framework-migrations

EntityType 'IdentityUserLogin' has no key defined


I am trying to use Individual User Accounts in my application. As im making migrations for any class related to the ApplicationUser-class (not ApplicationUser itself), I get the error that IdentityUserLogin, IdentityUserRole etc. has no key defined.

As it has been mentioned in other answers, I have added the call to OnModelCreating(), but without any luck:

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

    }

I have split my IdentityModel-class up, so the DbContext is in its class on its own. My IdentityModel-class looks like this:

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

The Certificate-class that I am trying to do migrations on can be seen here:

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

Solution

  • I found the answer!

    I misplaced the OnModelCreating method by putting it in the IdentityDbContext-class. Instead every class that is related to the IdentityUser-class has their own DbContext-class and all of the DbContext-classes has to have a call to the method shown below:

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