Entity framework ASP.Net.Core 2.2标识将ID从字符串更改为int
我正在尝试将Users表的ID从字符串(GUID)更改为int,并且非常困难。我看过很多例子,但它们似乎是针对Identity或vs的早期版本,并且由于许多原因它们不起作用 我要么得到一个编译器错误 “Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore 6[TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]”上的“Microsoft.AspNetCore.Identity.IdentityUser”违反了类型“TUser”的约束 或者,当我创建迁移时,仍然会得到ID的字符串列,而不是我所期望的int 我正在使用vs2019。Asp.Net.Core 2.2和Microsoft.AspNetCore.Identity 2.2Entity framework ASP.Net.Core 2.2标识将ID从字符串更改为int,entity-framework,asp.net-core,asp.net-identity,visual-studio-2019,Entity Framework,Asp.net Core,Asp.net Identity,Visual Studio 2019,我正在尝试将Users表的ID从字符串(GUID)更改为int,并且非常困难。我看过很多例子,但它们似乎是针对Identity或vs的早期版本,并且由于许多原因它们不起作用 我要么得到一个编译器错误 “Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore 6[TUser,TContext,TKey,TUserClaim,TUserLogin,TUserToken]”上的“Microsoft.AspNetCore.Ident
有人能帮我吗?谢谢 首先扩展
IdenityUser
类,如下所示,以便添加自定义属性:
public class ApplicationUser : IdentityUser<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);
}
}
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中编辑了mofied 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) }); builder.ConfigureServices((上下文,服务)=> { services.AddDbContext(options=>options.UseSqlServer(context.Configuration.GetConnectionString(“IdentityDbContextConnection”)); 服务.额外性() .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddDefaultUI();//为标识(登录、注册等)添加默认页面 }); 这里有一个教程:。 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) });