Search code examples
asp.netasp.net-mvcasp.net-identity

How to use different types for the Users table's pk and the roles table pk in Asp.net Identity


I'm trying to make my users table has a primary key of type Guid instead of the default string, my users entity looks like this:

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>

My context class looks like this

public class Context : IdentityDbContext<User, Role, Guid, UserLogin, UserRole, UserClaim>

But now, I have to make the Role Id of type Guid but not int like I want:

public class Role : IdentityRole<int, UserRole>//I can't do this because UserRole is tied to User which has Guid for TKey

Is there a way to make the User and Role use different types for the primary key?


Solution

  • Sadly, due to the extensibility of Identity you cannot do this. Your restriction on Key types is due to the actual Identity Context, when you define this you use this signature

    IdentityDbContext<TUser, TRole, TKey>
    

    It is the TKey value that drives the required primary key type for both user & role. You can have both be int, both be string, both be Guid etc, but you cannot split them to different types.