Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
During the whole day I just tried to add a single property to my AspNetUsers table generated by identity. I wanted that property to be a foreign key to a "Clients" table so that each client has it's own user and all the data of the client is in that respective clients table.
So here is what I've done:
In the IdentityModels:
public class ApplicationUser : IdentityUser
        public int ClientID { get; set; }
        public virtual Client Client { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
            // Tenga en cuenta que el valor de authenticationType debe coincidir con el definido en CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Agregar reclamaciones de usuario personalizado aquí
            return userIdentity;

And in the AccountController
public async Task<ActionResult> Register(RegisterViewModel model)
            if (ModelState.IsValid)
                //Para crear el cliente y el contexto
                var currentClient = new Client { Email = model.Email, Name = model.Name, Surname = model.Surname, PhoneNumber = model.PhoneNumber };
                var context = new TNEAContext();

                //Esto ya venia con el codigo
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, ClientID = currentClient.ClientID };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    // Para obtener más información sobre cómo habilitar la confirmación de cuenta y el restablecimiento de contraseña, visite
                    // Enviar correo electrónico con este vínculo
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirmar cuenta", "Para confirmar la cuenta, haga clic <a href=\"" + callbackUrl + "\">aquí</a>");

                    return RedirectToAction("Index", "Home");

            // Si llegamos a este punto, es que se ha producido un error y volvemos a mostrar el formulario
            return View(model);

And some other minor changes in the accountViewModel and the register view (in order to get those params).

I know that my problem is on the UserManager.CreateAsync method, but I don't know how to modify it, or what do I need to do in order to do that little change that is driving me mad :(
Mostafa Asaduzzaman 2-Jun-15 0:15am    
have you done database migration before running the application?
make sure that you have added the line
public DbSet< Client > Clients { get; set; } to your ApplicationDbContext class
Member 11733471 2-Jun-15 6:59am    
Yes, that DbSet was created in context
Mostafa Asaduzzaman 2-Jun-15 0:25am    
what is the error message you are getting?
kashif Atiq 2-Jun-15 1:59am    
what is the error?

1 solution

In reply to all comments, yeah I did migrations (but nothing was added) and this is the error I'm getting (and yes it is about migrations):[^]
Share this answer
Mostafa Asaduzzaman 2-Jun-15 17:35pm    
don't get as it is not in English
Member 11733471 2-Jun-15 17:56pm    
It says that the context has changed, so I might use migrations. But that is what I did. And still getting error. How can I force it to override the database with the prop added? because I know that the problemm is that the property is no being added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900