With ASP.NET Core, you have a very easy way to specify the data type that you want to use for Identity models.
The first step is to override the identity classes from <string> to <the data type you want>:
public class ApplicationUser : IdentityUser<Guid> { } public class ApplicationRole : IdentityRole<Guid> { }
Declare your database context using your classes and the data type you want:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder);
And in your startup class, declare an identity service using your models and declare the data type you want for primary keys:
services.AddIdentity<ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores<ApplicationDbContext, Guid>() .AddDefaultTokenProviders();
In ASP.NET authentication tables, the primary keys will still be in NVARCHAR, but in your application it will be the data type that you want. You can check this on the controller:
[HttpGet] public async Task<IActionResult> Test() { ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User); Guid userId = user.Id;
AdrienTorris Oct 17 '16 at 6:32 2016-10-17 06:32
source share