Using an Integer as the Primary Key in ASP.Net Core Identity
We generally use
However, by default, the
In this post, we detail how to change this to work with integers.
First create your custom models for
Then update your dbcontext to use your custom models.
Finally, we change the configuration of the Identity service in
If you are using migrations, you will need to update them too. If this is a fresh project, simply delete the default migrations and in the
In a future post, we will look at replacing SQL Server with Postgres.
auto-increment
integers (or serial in the case of Postgres) for our primary keys
.However, by default, the
Identity Framework
in MVC
is set to use string
.In this post, we detail how to change this to work with integers.
First create your custom models for
IdentityUser
and IdentityRole
.
public class ApplicationUser : IdentityUser<int> { } public class ApplicationRole : IdentityRole<int> { }
Then update your dbcontext to use your custom models.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int> { ... }
Finally, we change the configuration of the Identity service in
Startup
to match.
public void ConfigureServices(IServiceCollection services) { ... services.AddIdentity<ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores<ApplicationDbContext, int>() .AddDefaultTokenProviders(); ... }
If you are using migrations, you will need to update them too. If this is a fresh project, simply delete the default migrations and in the
package manager console
run
Add-Migration CreateIdentity.
In a future post, we will look at replacing SQL Server with Postgres.
Thanks for sharing Good Information
ReplyDeleteDot Net Online Training Bangalore
Will external login continue to work ?
ReplyDeleteThanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
nice post.dot net training
ReplyDelete