Link ASP.NET Identity users to user detail table

15,354

You can create one to one relationship between user and personal information and then create or update user using Application User Manager.

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public virtual PersonalInformation PersonalInformation { get; set; }
}

public class PersonalInformation 
{
    [Key, ForeignKey("User")]
    public string UserId { get;set; }

    public string FirstName { get; set; }

    // other fields...

    public virtual ApplicationUser User { get;set; }
}


// Create user      
var store = new UserStore<ApplicationUser>(context);
var manager =  new ApplicationUserManager(store);
var user = new ApplicationUser() { Email = "[email protected]", UserName = "username", PersonalInformation = new PersonalInformation { FirstName = "FirstName" } };
manager.Create(user, "Password123!");

// Update user
var store = new UserStore<ApplicationUser>(context);
var manager =  new ApplicationUserManager(store);
var user = manager.Users.FirstOrDefault(u => u.Id == id);
user.PersonalInformation.FirstName = "EditedName";
manager.Update(user);
Share:
15,354
Umer Waqar
Author by

Umer Waqar

Updated on June 17, 2022

Comments

  • Umer Waqar
    Umer Waqar almost 2 years

    I've used default MVC template with individual authorization. After running the application it automatically creates the required Identity tables. I've successfully registered a few users and all of them are stored in the AspNetUser table.

    Now what I want is the logged in user can add more information like first name, last name, address etc. For this I've created another table PersonalInformation with columns to store this information.

    I've also created an EditProfile Action in the ManageController.

    Now how can I link the ASP.NET Identity user to this table? Should I add an Id column as a foreign key to AspNetUser? Also how can I successfully store the edited information in "PersonalInformation" table along with the logged in user id?

  • Cathal O 'Donnell
    Cathal O 'Donnell about 8 years
    Hi I am also new to MVC. I am trying to do something similar to the question above. I have created my link table and now I'm trying to use code like your example to store data but can't figure where to put it. Where do I enter this code to do so?
  • ekad
    ekad over 7 years
    Please add some explanation how the above code solves the problem.
  • Max
    Max over 7 years
    What do not you understand? I put on duty as reference the fields aspNetUser
  • Samra
    Samra about 7 years
    Edin in my scenario, I want to add the personalinformation first and then the id in personalinformation should become the id in user
  • BishalG
    BishalG about 6 years
    I tried doing like this, but it throws me following exception: Member_AspUser_Source: : Multiplicity is not valid in Role 'Member_AspUser_Source' in relationship 'Member_AspUser'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. Is there anything missing ?