Problem creating relationship with user class (IdentityUser) using IdentityFramework

4

When I create any relationship with my user class:

 namespace Modelo.Cadastro
{
    public class Usuario : IdentityUser
    {
        [StringLength(250, ErrorMessage = "O nome de usuário deve conter no mínimo 3 caracteres", MinimumLength = 3)]
        [Required(ErrorMessage = "Informe o nome do usuário")]
        public string Nome { get; set; }

        //public virtual ICollection<Parecer> Pareceres { get; set; }
    }
}

I get the following error when EntityFramework attempts to create the database: One or more validation errors were detected during model generation:

  

Project02.Persistance.Contexts.IdentityUserLogin :: EntityType   'IdentityUserLogin' has no key defined

     

Project02.Persistance.Contexts.IdentityUserRole: EntityType   'IdentityUserRole' has no key defined.

I have already checked several solutions, including including the following lines in my context:

base.OnModelCreating(modelBuilder);

or even map the keys to the classes as well:

public class IdentityDbContextAplicacao : IdentityDbContext<Usuario>
    {
        public IdentityDbContextAplicacao() : base("IdentityDb")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id).Property(p => p.Name).IsRequired();
            modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
            modelBuilder.Entity<IdentityUserLogin>().HasKey(u => new { u.UserId, u.LoginProvider, u.ProviderKey });
        }

        static IdentityDbContextAplicacao()
        {
            Database.SetInitializer<IdentityDbContextAplicacao> (new IdentityDbInit());
        }
        public static IdentityDbContextAplicacao Create()
        {
            return new IdentityDbContextAplicacao();
        }

        public class IdentityDbInit : DropCreateDatabaseIfModelChanges<IdentityDbContextAplicacao>
        {
        }
    }

and nothing works! Any other output?

    
asked by anonymous 25.07.2016 / 20:30

2 answers

1

Because your class inherits from IdentityUser , the primary key of that object is set in the OnModelCreating method of the IdentityDbContext .

And it seems that by mistake, your context is deriving from DbContext .

You can see a more detailed response in English in this OS response .

    
25.07.2016 / 22:37
1

You probably are not finding the primary key in the IdentityUserLogin class. Above Id places [Key] and imports using System.ComponentModel.DataAnnotations;

Where would look something like this:

[Key]
public int Id { get; set; }
    
26.07.2016 / 14:00