Storing Custom Fields During User Registration In A Separate Table – C# ASP.NET Identity

There are tons of great tutorials on how to add custom fields during the registration process in ASP.NET MVC5 using User Identity.  I found the following articles to be quite useful.

  • https://blog.falafel.com/customize-mvc-5-application-users-using-asp-net-identity-2-0/
  • https://mwasimse.wordpress.com/2016/05/04/adding-custom-fields-on-user-registration-in-asp-net-mvc-5/

However, those articles will add the custom fields right into the AspNetUser tables which isn’t what I want.  I like to keep that table as close to standard as possible and store custom site data in it’s own table.  In order to do this, I hope sharing the following code will be useful to someone.

// IdentityModel.cs
public class ApplicationUser : IdentityUser
{
   public virtual UserProfileInfo UserProfileInfo { get; set; }
}

public class UserProfileInfo
{
   [Key, ForeignKey("ApplicationUser")]
   public string Id { get; set; }
   public virtual ApplicationUser ApplicationUser { get; set; }

   public string FirstName { get; set; }
   public string LastName { get; set; }
}

public class ApplicationDbContext : 
   IdentityDbContext<ApplicationUser>
{
   public DbSet<UserProfileInfo> UserProfileInfo { get; set; } 
}

As you can see from the code above, a new table called UserProfileInfo will be created with 

  • id (primary key, and foreign key to the AspNetUser table)
  • firstname
  • lastname

And that’s basically it.  Enjoy!