dotnet / dotnet/aspnetcore

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

Open
#56,409 0 comments 3 reactions 0 assignees View on GitHub
area-identity
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] 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:

```cs
// ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}

// ApplicationRole.cs
public class ApplicationRole : IdentityRole
{
}
```

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

```cs
public class AppDbContext(DbContextOptions options) : IdentityDbContext(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](https://github.com/dotnet/aspnetcore/blob/96559b6c1978e5a64e17fb48718cd60b4b6316f0/src/Identity/EntityFrameworkCore/src/IdentityDbContext.cs#L49), the TKey could be specified for each type or base class.

```cs
public class AppDbContext(DbContextOptions options) : IdentityDbContext, ApplicationRole>(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):

```cs
public class AppDbContext(DbContextOptions options) : IdentityDbContext, ApplicationRole>(options)
{
}
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.