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
?– Jéf Bueno
I didn’t actually implement anything for 'Identityuserlogin', I just tried a solution but it didn’t work in my case
– Luiz Henrique da Fonseca
What do you mean "not implemented anything"? You have created an empty class?
– Jéf Bueno
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– Luiz Henrique da Fonseca
You have two contexts created in the application?
– Jéf Bueno
Yes, I’m using one for the other models and one for Identity
– Luiz Henrique da Fonseca