Change the Primary Key for User table in Asp.NET Core Identity

 Change the Primary Key for User table in Asp.NET Core Identity


Here we will change the default primary key for the user table Asp.Net Core Identity in .NET 5. We will change the primary key from string to int.

Please use this when you are using a developement database.

This tutorial uses .NET 5 and EF Core 5.0.8

Create a new project

Create a new project of type ASP.NET Core 5.0 with enabling authentication type as Individual Accounts. When we are trying to customizing things in the Identity framework we may need to add new fields , change the default design and much more. The Identityframework uses Code first approach so we can customize more.

For this project I will use SQLite with EntityFrameworkCore version 5.0.8 but you can use SQL Server or any.

Remove the migration

Use the command remove-migration to remove the default created Migration


Add a new Class named AppUser Inheriting from identityUser

public class AppUser : IdentityUser<int>


Change the DbContext class

public class ApplicationDbContext : IdentityDbContext<AppUser, IdentityRole<int>, int>
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)

Change in the StartUp class

services.AddDbContext<ApplicationDbContext>(options =>

services.AddIdentity<AppUser, IdentityRole<int>>(options => options.SignIn.RequireConfirmedAccount = true)

Add a new migration

add-migration Init

Scaffold the View Pages for customization

Right Click on the Project - > New Scaffold Item - > Choose identity and All the View pages will be scaffolded.

Customize the design you want or Add new fields.

Generated Migration for AspNetUsers

Generated Migration for AspnetUsers

Table In Database


Change in the _LoginPartial partial view page

@inject SignInManager<AppUser> SignInManager
@inject UserManager<AppUser> UserManager

And You are done.

Run the app and check its working


Popular posts from this blog

Use SCSS with ASP.NET Core 5.x or 3.X

Building a Login Flow with .NET MAUI

PySpark Schema Generator - A simple tool to generate PySpark schema from JSON data