0
votes

I'm using ASP.Net Core 2.2 Identity and it generates all required tables for me where the primary key for AspNetUsers and AspNetRoles are string fields.

Here is my ApplicationUser:

public class ApplicationUser : IdentityUser
    {
        public int StaffId { get; set; }
        public string JobTitle { get; set; }
        public string FullName { get; set; }
    }

How can I set a custom integer field (staffId) as primary key of the AspNetUsers table and change the data type of AspNetRoles.Id from string to integer?

1
Please follow these steps to change the primary key to int. I guess this will solve your purpose and will not require an additional field for that. - Priyank Panchal
@PriyankPanchal, I've already followed those steps. That would help with changing the default Id's data type from string to int. What I'm looking for is, adding a custom field as primary key! - Kumail Jawadi
By custom field I'm assuming you mean custom type as a key. Try using the [Key] attribute on your field or using the generic IdentityUser passing in your custom type? That type would have to implement IEquatable - Richard Barker
Can I have User.Id and Role.Id as integer and claims as string? If yes, how to achieve this? - Kumail Jawadi

1 Answers

0
votes

In your context class you can set the primary key for the property you want to be the primary key:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ApplicationUser>()
                .HasKey(c => new { ApplicationUser.StaffId });
        }