Change the Primary Key for User table in Asp.NET Core Identity
Change the Primary Key for User table in Asp.NET Core Identity
Goal
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
Change the DbContext class
Change in the StartUp class
Add a new migration
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
Table In Database
Change in the _LoginPartial partial view page
And You are done.
Run the app and check its working
Comments
Post a Comment