Problem creating user class (Identityuser) relationship using Identityframework

Asked

Viewed 123 times

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.Persistencia.Contexts.Identityuserlogin: Entitytype 'Identityuserlogin' has no key defined

Project02.Persistencia.Contexts.Identityuserrole: Entitytype 'Identityuserrole' has no key defined.

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

base.OnModelCreating(modelBuilder);

or even map Keys to 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! Some other way out?

  • Can [Edit] your question and add the code from IdentityUserLogin?

  • I didn’t actually implement anything for 'Identityuserlogin', I just tried a solution but it didn’t work in my case

  • What do you mean "not implemented anything"? You have created an empty class?

  • I’m sorry if I misexpressed myself, but I just used this class from IdentityFramework to see if it solved my problems, I did not re-implement the class

  • You have two contexts created in the application?

  • Yes, I’m using one for the other models and one for Identity

Show 1 more comment

2 answers

1

Probably not finding the primary key in the class IdentityUserLogin. Above the ID places [Key] and matters to using System.ComponentModel.DataAnnotations;

Where would I be more or less like this:

[Key]
public int Id { get; set; }

1

As your class inherits from IdentityUser the primary key of that object is defined in the method OnModelCreating class Identitydbcontext.

And apparently by mistake its context is derived from DbContext.

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

  • vappolinario, I edited my question and inserted my class that inherits from the IdentityDbContext<Usuario>, I still don’t understand the solution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.