Help to write to USERS and Aspnetusers at once

Asked

Viewed 211 times

0

Help please. I’ve spent my whole day redoing various parts of the system, trying a lot of different solutions but ended up without ideas.

I have a solution with several applications. 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 tables of Identity.

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 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 didn’t work because Aspnetuser has that GUID (string Id) and I would have to remake a lot of relationships in the database. So I kept both tables. I added an Idaspnetuser to my USUARIOS table and they got a 1 to 1 relationship.

I made a View that works with Applpicationuser to register the user and was working ok. But I thought it was strange, because I was doing 1 userManager.add and then 1 Contextoperasistencia.USUARIOS.Add. As the relationship between them is 1 to 1, I then had the brilliant idea of adding a virtual property USUARIOS {get; set} in Applicationuser and that’s when I started to have mistakes and lapse.

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. Define the key for this Entitytype. Aspnetuserlogins: Entitytype: Entityset 'Aspnetuserlogins' is based on type 'Aspnetuserlogins' that has no Keys defined."

If I mark the Virtual USUARIOS property inside Applicationuser as [Notmapped], the application works again, but then I have to save Aspnetid and USUARIO 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);

1 answer

1


I think you could do the following:
Keep only the Aspnetusers table and include the properties of your old table ( Including the ID, leave it as a Unique field in the database and use it to make the joins and everything).

In case you don’t want to do this, consider this:
Entityframework understands a virtual property as some kind of relationship. If you define a property in the Applicationuser class, in the Users class you need to define who is the Foreign Key of the table for it to map. inserir a descrição da imagem aqui


  • Thank you Will. I really found it easier to keep a single table. I found an excellent tutorial that even helped me to pass the Id’s from Identity to Int. I did it in a test application and it worked. When I replicated in my application I actually get an error 'The Entity types 'Customrole' and 'Aspnetroles' cannot share table 'Aspnetroles'. I even made another post with this question: https://answall.com/questions/257544/erro-the-entity-types-customrole--aspnetroles-cannot-sharetable-aspnet

Browser other questions tagged

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