Question

I have set Identity so I have separate table for user profile data (firstname, lastname etc.).
Problem is that I have joined those tables via Email field.
I want to change this so I can connect tables by UserID instead.
What I need is to get new userID for created user and use that as foreign key to UserProfileInfo object.

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public string Email { get; set; }
    public string ConfirmationToken { get; set; }
    public bool IsConfirmed { get; set; }
    public virtual UserProfileInfo UserProfileInfo { get; set; }
}

public class UserProfileInfo
{
    public int Id { get; set; }
    public string EmailId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

When I create new user I have:

var user = new ApplicationUser()
{
    UserName = model.UserName,
    Email = model.Email,
    ConfirmationToken = confirmationToken,
    IsConfirmed = false,
    UserProfileInfo = new UserProfileInfo { EmailId = model.Email }
};

var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
    SendEmailConfirmation(model.Email, model.UserName, confirmationToken);
    return RedirectToAction("RegisterStepTwo", "Account");
}
else...
Was it helpful?

Solution

Why are you doing a join in the first place? ApplicationUser is your user, and the whole point of inheriting from IdentityUser is to allow you extend the user object. Just put your properties like FirstName and LastName directly on ApplicationUser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top