从历史上看,当我想在 AspNetUsers 表中添加一列或多列时,我将遵循的工作流程如下:
1.创建一个ApplicationUser类,从它继承IdentityUser。
2.在应用用户类中添加新的属性
3:更新applicationDbContext继承自:IdentityDbContext
4:更改启动代码中对 IdentityUser 的任何引用,例如:Startup.cs/Global 等
5:Add-Migration MigrationName
6:更新数据库
这将为新列生成向上/向下脚本并将该列添加到我的数据库中。
但是,我已经启动了一个新的 Blazor 服务器端 Web 应用程序并执行了上述步骤无济于事。
任何人都可以看到我在这里遗漏的任何东西,我过去已经这样做了足够多的时间,我发现我可能错过了一些东西很奇怪,但一切皆有可能。希望有人可以提供帮助,请参阅下面我为实现这一目标而更改的代码。
应用程序数据库上下文代码:
namespace ExtendingBlazorIdentity.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
应用用户类:namespace ExtendingBlazorIdentity.Data
{
public class ApplicationUser : IdentityUser
{
string NickName { get; set; }
}
}
启动文件 namespace ExtendingBlazorIdentity
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
最佳答案
你的属性(property)不是公开的。
public class ApplicationUser : IdentityUser
{
public string NickName { get; set; }
}
关于entity-framework - 如何在 Blazor 服务器端 Web 应用程序中扩展 IdentityUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64945728/