In this tutorial, we will show you how to change the Primary key of the Users table to another type in ASP.NET Core Identity. We will change the Primary key type to int in our example
Table of Contents
Before Changing the Primary Key
Changing the Primary Key data type of database, which already has data in it is a big headache. The Problem is bigger if consider changing the PK type of a table that is already in Production.
This tutorial assumes that you are working on a development database.
Change the primary key type
Create a new Project
Create a new ASP.NET Core Web App in VS 2019, with .NET 5.0 & Identity enabled (ASPNetCoreIdentityPK
)
1 2 3 4 5 | "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=ASPNetCoreIdentityPK;Trusted_Connection=True;MultipleActiveResultSets=true" }, |
Remove the Migration
First, we need to remove the migration.
1 2 3 | remove-migration |
Context
The ApplicationDbContext
inherits from the IdentityDbContext
.
Change this to
1 2 3 | public class ApplicationDbContext : IdentityDbContext |
this
1 2 3 4 | public class ApplicationDbContext : IdentityDbContext<IdentityUser<int>,IdentityRole<int>,int> |
We are specifying int
as Primary Key of the IdentityUser
table. We also need to change it in IdentityRole
table as it is an FK here.
Startup
Change the IdentityUser
1 2 3 4 | services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>(); |
to IdentityUser<int>
1 2 3 4 | services.AddDefaultIdentity<IdentityUser<int>>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>(); |
Update the database
1 2 3 4 | add-migration intial update-database |
Verify
Login Partial
Open the Pages/Shared/_LoginPartial.cshtml
and change this
1 2 3 4 | @inject SignInManager<IdentityUser> SignInManager @inject UserManager<IdentityUser> UserManager |
This
1 2 3 4 | @inject SignInManager<IdentityUser<int>> SignInManager @inject UserManager<IdentityUser<int>> UserManager |
Thats it.
Run the App and check
Read More
- ASP.NET Core Tutorial
- Authentication in ASP.NET Core
- Cookie Authentication in ASP.NET Core
- Introduction to ASP.NET Core Identity
- ASP.NET Core Identity Tutorial From Scratch
- Sending Email Confirmation in ASP.NET Core
- Add Custom Fields to the user in ASP.NET Core Identity
- Change Primary key in ASP.NET Core Identity
- JWT Authentication in ASP.NET Core
- Introduction to Authorization
- Simple Authorization using Authorize attribute
- Adding & Managing Claims in ASP.NET Core Identity
- Claim Based Authorization in ASP.NET Core
- Policy-based Authorization
- Resource-Based Authorization
Hi, I have problem, I do everything from your tutorial and when I open Register or Login Page I receive:
InvalidOperationException: Unable to resolve service for type ‘Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ while attempting to activate ‘RaterestaurantMVC.Web.Areas.Identity.Pages.Account.RegisterModel
Thank you, simple and clear. However, I struggle with extending the IdentityUser with id as int with my own properties in an ApplicationUser. Can you help?
thank you
but also you must change the _loginPartial like this
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager