Adding columns to AspNetUsers table in ASP.NET Core 2.0

We know that it is quite easy to have an application in ASP.NET Core 2.0 that works with a user system. You only have to press the Change Authentication button when you are creating the application, and select the desired option. In our case, we will work with the Individual User Accounts option: Store user accounts in-app. If you are using dotnet cli, then you can use the “-au Individual” flags when creating the project. The idea of this option is that the information of our users will reside in our system.

If you have used this option, you know that the system creates several tables for you: the user table, the roles table, claims, logins, among others. Users’ information is stored in the user table. You may want to modify this table to store extra information. That is possible to do it. Let’s see the how it works.

To do this we only have to:

  1. Modify the ApplicationUser class
  2. Add the migration
  3. Apply the migration

ApplicationUser

If you created your project as I indicated in the introduction to this post (Individual User Accounts: Store user accounts in-app), you will see a class called ApplicationUser in the Models folder. This class is a model which represents users of your application.

If you have not touched it, you will see that it is empty. This is where we can place the user information that we want to save. Suppose we want to save the user’s date of birth and country of origin. For that then we will create two properties in the ApplicationUser class:


public class ApplicationUser: IdentityUser
{
   public DateTime Birthdate {get; set; }
   public string Country {get; set; }
}

These properties will be transformed into columns with the migrations.

Adding Migration

We remember that migrations are incremental changes that we make to our database using Entity Framework. The idea is that when you make changes to your models (such as ApplicationUser), and you want those changes to be replicated in your database, you will create a migration which documents the changes that will be made at the database level. Then you must apply these changes in the database. Let’s start with adding the migration. Do one of the following depending on whether you use Visual Studio or the dotnet cli:

  • If you are using Visual Studio, go to the Package Manager Console, which you can reach via: Tools> Nuget Package Manager> Package Manager Console. And enter the following command:

Add-Migrations User_BirthDate_Country

  • If you are using the dotnet cli, then use the following command:

dotnet ef migrations add User_BirthDate_Country

If you wish, you can visualize the previous migration to apply it in your database. For that, go to the User_BirthDate_Country class that was created in your project. In it we can see that it is indicated that the Birthdate and Country columns will be added in the AspNetUsers table.

Applying Migration

Now that we have the migration created, we can apply it:

  • If you use Visual Studio, go to the Package Manager Console and use the following command:

update-database

  • If you use the dotnet cli, use the following command:

dotnet ef database update

Now with this we will see the changes in the table:

asp net core added column to user tablePNG

Summary

Modify the ApplicationUser class to add columns to the AspNetUsers table.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s