I have followed this article to modify Primary Key data type of the Identity classes from String to Guid and it does create the uniqueidentifier
data type for respective columns in the SQL Server instead of the default NVARCHAR
but I am unable to use the UserManager
and SignInManager
classes as they refuse to accept the customized ApplicationUser class (that extends IdentityUser, as described in the article) instead of the default IdentityUser
class. The article does not go into much detail and by looking at answers to other similar questions on SO like this one it seems that either I have to change all the classes of the Identity to accomplish this goal or use the 'other' UserManager
class which I am not entirely sure how to.
My customized Identity models look like this:
public class IdentityModels
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(GetConnectionString());
}
private static string GetConnectionString()
{
const string databaseName = "testname";
const string databaseUser = "testuser";
const string databasePass = "testpass";
return $"Server=localhost;" +
$"database={databaseName};" +
$"uid={databaseUser};" +
$"pwd={databasePass};" +
$"pooling=true;";
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Core Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Core Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
What is the correct way to change the data type for the Primary Key attribute of the Identity Classes in Identity 2.0 so that UserManager
and SignInManager
accept the modifications?
Based on the documentation here:
You will need to create custom classes that derive from each asp.net identity type which all have a TKey generic parameter which will allow you to specify the type for the primary key. Here are all the signatures of example custom identity classes:
public class ApplicationUserToken : IdentityUserToken<Guid> { }
public class ApplicationUserLogin : IdentityUserLogin<Guid>{ }
public class ApplicationRoleClaim : IdentityRoleClaim<Guid>{ }
public class ApplicationUserRole : IdentityUserRole<Guid>{ }
public class ApplicationUser : IdentityUser<Guid>{ }
public class ApplicationUserClaim : IdentityUserClaim<Guid>{ }
public class ApplicationRole : IdentityRole<Guid> { }
Then in your application DB context inherit from IdentityDbContext replacing all the identity classes with the custom ones:
public class IdentityDbContext<TUser, TRole, TKey> : IdentityDbContext<TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
Like this:
public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken> { }
Then add this to your Startup.cs file:
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
Last you can run a new migration and update your data base via the console:
PM> Add-Migration cmdlet Add-Migration at command pipeline position 1 Supply values for the following parameters: Name: Guid-PK PM> Update-Database -verbose
Cheers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.