Help please. I lost all day redoing various parts of the system, trying a lot of different solutions but I ended up with no ideas.
I have a multi-application solution. One of these applications (where I try to isolate data persistence) has the context created with DataBase First in the Entity Framework. Note: I had already included in the database, with a create script, the Identity tables.
In another layer of my solution I added a pure MVC application without user control. So I added via NuGet the Entity Framework and Identity.
And finally I created ApplicationUser, IdentityDbContext and ApplicationRoleManager:
public partial class ApplicationUser : IdentityUser
{
[Display(Name = "Usuario", Description = "Identificação para Login")]
[Required(ErrorMessage = "000027 - O Login não pode ser vazio ou nulo.")]
[MaxLength(30, ErrorMessage = "000028 - O Login pode ter no máximo 30 caracteres.")]
[DuplicadoNaBase("AspNetUsersDao", "SelecionarPorLogin", ErrorMessage = "000031 - O Login informado já existe na base de dados.")]
public string LOGIN { get; set; }
[NotMapped]
[Display(Name = "Senha", Description = "Senha para Login")]
[Required(ErrorMessage = "000029 - A senha não pode ser vazia ou nula.")]
[MaxLength(30, ErrorMessage = "000030 - A senha pode ter no máximo 30 caracteres.")]
[DataType(DataType.Password)]
public string SENHA { get; set; }
public virtual USUARIOS USUARIOS { get; set; }
[NotMapped]
public string Perfil { get; set; }
}
public class GPSdEntitiesIdentity : IdentityDbContext<ApplicationUser>
{
public GPSdEntitiesIdentity() : base("IdentityConnection")
{
}
}
public class ApplicationRoleManager : RoleManager<IdentityRole, string>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore<IdentityRole>(new GPSdEntities()));
}
}
Well basically my problem happens because I already had a database with a USER table. Initially I tried to merge my USER table with AspNetUser. But it did not work because AspNetUser has that GUID (string Id) and I would have to redo a lot of relationships in the database. So I kept the two tables. I added an IdAspNetUser to my USERS table and they got a 1 to 1 relationship.
I made a View that works with ApplpicationUser to register the user and it was working ok. But I thought it was strange, because I was doing 1 userManager.add and then 1 ContextPerformance.USUARIOS.Add. As the relationship between them is 1 to 1, then I had the brilliant idea of adding in the ApplicationUser a virtual property USERS USERS {get; set} and that's when I started having mistakes and going around.
When I try:
private UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new GPSdEntitiesIdentity()));
List<ApplicationUser> ApplicationUsers = new List<ApplicationUser>();
ApplicationUsers = userManager.Users.ToList();
I'm getting the following error: System.Data.Entity.ModelConfiguration.ModelValidationException - "One or more validation errors were detected during model generation: GPSd.Web.MVC.IdentityContext.AspNetUserLogins:: EntityType 'AspNetUserLogins' has no key defined. Set the key for this EntityType. AspNetUserLogins: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNetUserLogins' that has no keys defined. "
If I mark the USERS Virtual property within the ApplicationUser as [NotMapped], the application works again, but then I have to save AspNetId and USER separately ...
I'm trying to do something simple like:
ApplicationUser identityUser = new ApplicationUser
{
UserName = model.LOGIN,
LOGIN = model.LOGIN,
SENHA = model.SENHA,
USUARIOS = model.USUARIOS
};
IdentityResult resultado = userManager.Create(identityUser, model.SENHA);