Skip to content

Separate type of the primary key for users, roles, etc in the identity entities  #56409

Open
@latuconsinafr

Description

@latuconsinafr

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to create a project following the Domain-Driven Design (DDD) approach (to solve several problems in complex domains). Most of the time when writing the model/entity using this approach, people will avoid primitives for IDs (for example: UserId for User or RoleId for Role).

Until I tried to extend the existing IdentityUser and IdentityRole for example to something like this:

// ApplicationUser.cs
public class ApplicationUser : IdentityUser<UserId>
{
}

// ApplicationRole.cs
public class ApplicationRole : IdentityRole<RoleId>
{
}

Then I stuck into the problem when I tried to update the ApplicationDbContext:

public class AppDbContext(DbContextOptions<AppDbContext> options) : IdentityDbContext<ApplicationUser, ApplicationRole, UserId>(options) // Here is the problem TKey will be applied to Role as well
{
}

Describe the solution you'd like

Rather than using all the TKey for all base class like here, the TKey could be specified for each type or base class.

public class AppDbContext(DbContextOptions<AppDbContext> options) : IdentityDbContext<ApplicationUser<UserId>, ApplicationRole<RoleId>>(options)
{
}

Or probably the team considered the pros and cons for every data type that is applied to the primary key and then decided to use another data type for every type or base class (in this case using primitives, int auto increment vs guid):

public class AppDbContext(DbContextOptions<AppDbContext> options) : IdentityDbContext<ApplicationUser<int>, ApplicationRole<Guid>>(options)
{
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-identityIncludes: Identity and providers

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions