更新时间:2023-02-25 10:41:40
使用 ASP.NET Core,您可以通过一种非常简单的方法为 Identity 模型指定所需的数据类型.
With ASP.NET Core, you have a very simple way to specify the data type you want for Identity's models.
第一步,覆盖 < 中的标识类字符串> 到 :
First step, override identity classes from < string> to < 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);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
在您的启动类中,使用您的模型声明身份服务并声明主键所需的数据类型:
And in your startup class, declare the identity service using your models and declare the data type you want for the primary keys :
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext, Guid>()
.AddDefaultTokenProviders();