title: Save ASP.NET core Identity User attributes in another table
body: <p>I am developing an ASP .NET core web application. I employ <strong>Identity UI framework</strong> for the user registration and authorization in the application. I inherit Identity User to my <code>ApplicationUser</code> class as follows. It creates a table called as <code>ApplicationUser</code> and save the relevant data under the given attributes successfully: (I am using <code>Microsoft SQL</code> database)</p>
  <p><em><strong>ApplicationUser.cs</strong></em></p>
  <pre><code>using Microsoft.AspNetCore.Identity;
  using System;
  using System.Collections.Generic;
  
  #nullable disable
  
  namespace WebApp.Models
  {
          public partial class ApplicationUser : IdentityUser&lt;int&gt;
          {
              public ApplicationUser()
              {
              }
  
              public string FirstName { get; set; }
              public string MiddleName { get; set; }
              public string LastNam { get; set; }
              public DateTime? DateOfBirth { get; set; }
              public string Bio { get; set; }
  
              public virtual UserAddress UserAddress { get; set; }
          }
  }
  </code></pre>
  <p>Then I implemented <code>UserAddress</code> model class as follows. It creates another table in the database named as <strong>&quot;UserAddress&quot;</strong>\</p>
  <p><strong>UserAddress.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  
  #nullable disable
  
  namespace WebApp.Models
  {
      public partial class UserAddress
      {
          public int UserId { get; set; }
          public string BuildingNo { get; set; }
          public string Street { get; set; }
          public string City { get; set; }
          public string State { get; set; }
          public string ZipCode { get; set; }
  
          public virtual ApplicationUser User { get; set; }
      }
  }
  </code></pre>
  <p>Next in under <strong>Areas</strong> folder in Identity UI frame work, I change the <strong>Index.cshtml</strong> file as follows. i inserting new entry to enter the <code>building number</code> of the user, that should be saved in <code>UserAddress</code> table in database.</p>
  <p><em><strong>Index.cshtml</strong></em></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>@page
  @model IndexModel
  @{
      ViewData["Title"] = "Profile";
      ViewData["ActivePage"] = ManageNavPages.Index;
  }
  
  &lt;h4&gt;@ViewData["Title"]&lt;/h4&gt;
  &lt;partial name="_StatusMessage" model="Model.StatusMessage" /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-12"&gt;
          &lt;form id="profile-form" method="post"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
              &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.UserName"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.UserName" class="form-control" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.FirstName"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.FirstName" class="form-control" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.MiddleName"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.MiddleName" class="form-control" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.LastNam"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.LastNam" class="form-control" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.DateOfBirth"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.DateOfBirth" class="form-control" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group col-md-6"&gt;
                      &lt;label asp-for="Input.PhoneNumber"&gt;&lt;/label&gt;
                      &lt;input asp-for="Input.PhoneNumber" class="form-control" /&gt;
                      &lt;span asp-validation-for="Input.PhoneNumber" class="text-danger"&gt;&lt;/span&gt;
                  &lt;/div&gt;
               &lt;/div&gt;
               &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="form-group col-md-6"&gt;
                          &lt;label asp-for="Input.BuildingNo"&gt;&lt;/label&gt;
                          &lt;input asp-for="Input.BuildingNo" class="form-control" /&gt;
                  &lt;/div&gt;
               &lt;/div&gt;
              &lt;div class="=row d-flex flex-row"&gt;
                  &lt;div class="do-md-6"&gt;
                      &lt;button id="update-profile-button" type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
           &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  @section Scripts {
      &lt;partial name="_ValidationScriptsPartial" /&gt;
  }</code></pre>
  </div>
  </div>
  </p>
  <p>And in the <strong>Index.cshtml.cs</strong> file, I tried to save the <code>building number</code> in the <code>UserAddress</code> table as follows but it fails to enter the data into the table.</p>
  <p><em><strong>Index.cshtml.cs</strong></em></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Identity;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using WebApp.Models;
  
  namespace WebApp.Areas.Identity.Pages.Account.Manage
  {
      public partial class IndexModel : PageModel
      {
          private readonly UserManager&lt;ApplicationUser&gt; _userManager;
          private readonly SignInManager&lt;ApplicationUser&gt; _signInManager;
  
          public IndexModel(
              UserManager&lt;ApplicationUser&gt; userManager,
              SignInManager&lt;ApplicationUser&gt; signInManager)
          {
              _userManager = userManager;
              _signInManager = signInManager;
          }
  
          public string BuildingNo { get; set; }
          public string Username { get; set; }
  
          [TempData]
          public string StatusMessage { get; set; }
  
          [BindProperty]
          public InputModel Input { get; set; }
  
          public class InputModel
          {
  
              [DataType(DataType.Text)]
              [Display(Name = &quot;User Name&quot;)]
              public string UserName { get; set; }
  
              [DataType(DataType.Text)]
              [Display(Name = &quot;First name&quot;)]
              public string FirstName { get; set; }
  
  
              [DataType(DataType.Text)]
              [Display(Name = &quot;Middle name&quot;)]
              public string MiddleName { get; set; }
  
  
              [DataType(DataType.Text)]
              [Display(Name = &quot;Last name&quot;)]
              public string LastNam { get; set; }
  
  
              [Display(Name = &quot;Date of Birth&quot;)]
              [DataType(DataType.Date)]
              public DateTime DateOfBirth { get; set; }
  
              [Phone]
              [Display(Name = &quot;Phone number&quot;)]
              public string PhoneNumber { get; set; }
  
              [Display(Name = &quot;Building No:&quot;)]
              [DataType(DataType.Text)]
              public string BuildingNo { get; set; }
          }
  
          private async Task LoadAsync(ApplicationUser user)
          {
              var userName = await _userManager.GetUserNameAsync(user);
              var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
              Username = userName;
              Input = new InputModel
              {
                  UserName = user.UserName,
                  FirstName = user.FirstName,
                  MiddleName = user.MiddleName,
                  LastNam = user.LastNam,
                  PhoneNumber = phoneNumber, 
              };
              
          }
  
          public async Task&lt;IActionResult&gt; OnGetAsync()
          {
              var user = await _userManager.GetUserAsync(User);
              if (user == null)
              {
                  return NotFound($&quot;Unable to load user with ID '{_userManager.GetUserId(User)}'.&quot;);
              }
  
              await LoadAsync(user);
              return Page();
          }
  
          public async Task&lt;IActionResult&gt; OnPostAsync()
          {
              var user = await _userManager.GetUserAsync(User);
              if (user == null)
              {
                  return NotFound($&quot;Unable to load user with ID '{_userManager.GetUserId(User)}'.&quot;);
              }
  
              if (!ModelState.IsValid)
              {
                  await LoadAsync(user);
                  return Page();
              }
  
              var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
              if (Input.PhoneNumber != phoneNumber)
              {
                  var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
                  if (!setPhoneResult.Succeeded)
                  {
                      StatusMessage = &quot;Unexpected error when trying to set phone number.&quot;;
                      return RedirectToPage();
                  }
              }
              if(Input.FirstName != user.FirstName)
              {
                  user.FirstName = Input.FirstName;
              }
  
              if (Input.MiddleName != user.MiddleName)
              {
                  user.MiddleName = Input.MiddleName;
              }
              if (Input.LastNam != user.LastNam)
              {
                  user.LastNam = Input.LastNam;
              }
              if (Input.DateOfBirth != user.DateOfBirth)
              {
                  user.DateOfBirth = Input.DateOfBirth;
              }
              if (Input.UserName != user.UserName)
              {
                  user.UserName = Input.UserName;
              }
              
              user.UserAddress.BuildingNo = Input.BuildingNo; // I tried to enter the building address to the UserAddress table by using this code
  
              await _userManager.UpdateAsync(user);
  
              await _signInManager.RefreshSignInAsync(user);
              StatusMessage = &quot;Your profile has been updated&quot;;
              return RedirectToPage();
          }
      }
  }
  </code></pre>
  <p>Please ignore the above lengthy <code>Index.cshtml.cs</code> code I have provided, I have commented near the only code that I used to insert the input  <code>building number</code> to the UserAddress table. which is:<br />
  <code>user.UserAddress.BuildingNo = Input.BuildingNo;</code></p>
  <p><strong>This is the part of view of the entry:</strong> <a href="https://i.stack.imgur.com/ovynM.png" rel="noreferrer">The entry view</a><br />
  <strong>And this is the error I am getting when above code is run:</strong> <a href="https://i.stack.imgur.com/K6zko.png" rel="noreferrer">The error message</a></p>
  <p><em><strong>For the record:</strong></em> all the fields in <code>ApplicationUser</code> table is updated. but this error occurs when I am trying to insert data into <code>UserAddress</code> table.</p>
  <p><em><strong>I am pretty user this is a very simple question for a person who knows ASP.NET core identity user very well.</strong></em></p>
  <p>I kindly request if somebody can please help me to save the input <code>building number</code> in another table named <code>User Address</code>?</p>
  <p>Thanks in advance !!!</p>