Configure the data type of the primary keys using ASP.NET Core Identity

By default, ASP.NET Core Identity uses string data type for the primary keys, because like Rick Anderson explained, Microsoft and ASP.NET doesn't want to involve in your business logic, this is your concern, so they are using string data type who is not a strong-typed data-type and allows you to cast him easily.

If this choice from ASP.NET's team is totally understandable, you often need (or just want) other data type that string for your primary keys, like integers or Guids. This is very simple to implement with ASP.NET Core, you have just a few lines of code to write.


This implementation doesn't change the data type of the columns in the database, the columns of the primary keys are still NVARCHAR(450) (yes, this is huge) but your objects will be more easy to manipulate because you won't have to cast their identifiers every time you'll have to use them


[HttpGet]
public async Task<IActionResult> Test()
{
    ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);
    Guid userId = user.Id; // No cast necessary here because user's id property is a Guid, and not a string
    throw new NotImplementedException("It's was just to test something very pleasant and very easy to do");
}
16/10/2016
  • ASP.NET Core
  • Identity