ASP.Net.Core 2.2 Identity 将ID从string改为int
ASP.Net.Core 2.2 Identity change ID from string to int
我正在尝试将用户 table 的 ID 从字符串 (GUID) 更改为 int 并且非常困难。我看过很多示例,但它们似乎适用于早期版本的 Identity 或 vs,并且由于多种原因它们不起作用。
我要么遇到编译器错误
'Microsoft.AspNetCore.Identity.IdentityUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore 6[TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]' violates the constraint of type 'TUser'.
或者当我创建迁移时,我仍然得到 ID 的字符串列,而不是我预期的 int。
我用的是vs2019。 Asp.Net.Core 2.2 和 Microsoft.AspNetCore.Identity 2.2
有人能帮帮我吗?谢谢!
首先扩展 IdenityUser
class class 如下,以便您可以添加自定义属性:
public class ApplicationUser : IdentityUser<int>
{
}
然后扩展 IdentityRole
class 如果您也在应用程序中使用 Role
。即使您不想使用它,也可以安全地保存它:
public class ApplicationRole : IdentityRole<int>
{
}
现在您的 ApplicationDbContext
应该如下所示:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
现在在你的ConfigureServices
方法中Startup
class如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
任务完成!现在 运行 一个全新的迁移并应用它。
@TanvirArjel 解决方案也可以在我的项目中使用 .NET Core 3.1 进行修改。我通过脚手架添加了身份。
我没有编辑 ConfigureServices(),而是编辑了 IdentityHostingStartup.cs 并在 IdentityHostingStartup.cs:
中修改了 Configure()
builder.ConfigureServices((context, services) =>
{
services.AddDbContext(options => options.UseSqlServer(context.Configuration.GetConnectionString("IdentityDbContextConnection")));
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders()
.AddDefaultUI(); //add default razor pages for identity (login, register, etc)
});
我正在尝试将用户 table 的 ID 从字符串 (GUID) 更改为 int 并且非常困难。我看过很多示例,但它们似乎适用于早期版本的 Identity 或 vs,并且由于多种原因它们不起作用。
我要么遇到编译器错误
'Microsoft.AspNetCore.Identity.IdentityUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore 6[TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]' violates the constraint of type 'TUser'.
或者当我创建迁移时,我仍然得到 ID 的字符串列,而不是我预期的 int。
我用的是vs2019。 Asp.Net.Core 2.2 和 Microsoft.AspNetCore.Identity 2.2
有人能帮帮我吗?谢谢!
首先扩展 IdenityUser
class class 如下,以便您可以添加自定义属性:
public class ApplicationUser : IdentityUser<int>
{
}
然后扩展 IdentityRole
class 如果您也在应用程序中使用 Role
。即使您不想使用它,也可以安全地保存它:
public class ApplicationRole : IdentityRole<int>
{
}
现在您的 ApplicationDbContext
应该如下所示:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
现在在你的ConfigureServices
方法中Startup
class如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
任务完成!现在 运行 一个全新的迁移并应用它。
@TanvirArjel 解决方案也可以在我的项目中使用 .NET Core 3.1 进行修改。我通过脚手架添加了身份。
我没有编辑 ConfigureServices(),而是编辑了 IdentityHostingStartup.cs 并在 IdentityHostingStartup.cs:
中修改了 Configure()builder.ConfigureServices((context, services) => { services.AddDbContext(options => options.UseSqlServer(context.Configuration.GetConnectionString("IdentityDbContextConnection"))); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddDefaultUI(); //add default razor pages for identity (login, register, etc) });