You can leave the AspNetUsers table intact and create a new table to store additional information (related to the source). This way you will not violate the existing code in the membership provider.
The AspNetUsers source table contains: [Id], [E-mail], [EmailConfirmed], [PasswordHash], [SecurityStamp], [Phone Number], [PhoneNumberConfirmed], [TwoFactorEnabled], [LockoutEndDateUtc], [LockoutEnabled], [AccessFailedCount ], [username] p>
A new table for storing additional data may have, for example: [Id], [UserId] [DateOfBirth], [Biography], etc. Where [UserId] is the foreign key of the AspNetUsers table.
One of the advantages of this approach is that you can create several types of users, each of which stores the information associated with it in another table, while the general data is still in the original table.
How to do:
- First update RegisterViewModel to contain the additional data you want.
Update the Register method in the account controller, here the original method is updated with the code to insert new profile data:
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Start of new code ---------------------------------------- // Get Id of newly inserted user int userId = user.Id; // Get Id of newly inserted user // Create a profile referencing the userId AddUserProfile(userId, model); // End of new code ---------------------------------------- await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } return View(model); }
- Implement the AddUserProfile method (int userId, RegisterViewModel) as you wish. You will collect additional data from the model object along with userId and save the new profile object in the database.
source share