AspNetCore.Docs/aspnetcore/security/authentication/identity-primary-key-config...

2.9 KiB

title author description ms.author ms.date uid
Configure Identity primary key data type in ASP.NET Core AdrienTorris Learn about the steps for configuring the desired data type used for the ASP.NET Core Identity primary key. scaddie 09/28/2017 security/authentication/identity-primary-key-configuration

Configure Identity primary key data type in ASP.NET Core

ASP.NET Core Identity allows you to configure the data type used to represent a primary key. Identity uses the string data type by default. You can override this behavior.

Customize the primary key data type

  1. Create a custom implementation of the IdentityUser class. It represents the type to be used for creating user objects. In the following example, the default string type is replaced with Guid.

    [!code-csharp]

  2. Create a custom implementation of the IdentityRole class. It represents the type to be used for creating role objects. In the following example, the default string type is replaced with Guid.

    [!code-csharp]

  3. Create a custom database context class. It inherits from the Entity Framework database context class used for Identity. The TUser and TRole arguments reference the custom user and role classes created in the previous step, respectively. The Guid data type is defined for the primary key.

    [!code-csharp]

  4. Register the custom database context class when adding the Identity service in the app's startup class.

    ASP.NET Core 2.x

    The AddEntityFrameworkStores method doesn't accept a TKey argument as it did in ASP.NET Core 1.x. The primary key's data type is inferred by analyzing the DbContext object.

    [!code-csharp]

    ASP.NET Core 1.x

    The AddEntityFrameworkStores method accepts a TKey argument indicating the primary key's data type.

    [!code-csharp]


Test the changes

Upon completion of the configuration changes, the property representing the primary key reflects the new data type. The following example demonstrates accessing the property in an MVC controller.

[!code-csharp]