entity-framework - 如何在 Blazor 服务器端 Web 应用程序中扩展 IdentityUser

标签 entity-framework asp.net-core asp.net-identity blazor

从历史上看,当我想在 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/

相关文章:

c# - Entity Framework 中的 Sql Char(13) 和 isnull 的替代品是什么?

c# - 如何将 ILogger 传递给我的过滤器

azure - 使用企业代理实现 Azure Key Vault

.net - 在.net core中更新具有身份的用户角色而无需重新登录

c# - 将 UserId 分配给外键 = null

asp.net-mvc - 可访问性不一致 : property type in DbContext

c# - MVC Net Core 侧边栏导航菜单放置在_Layout.cshtml

c# - 如何在 Controller 的构造函数中获取当前用户的数据?

c# - 如何在使用 asp.net Identity 关闭浏览器后保留登录信息?

c# - 使用 .Take() 方法对实体的 Linq 非常慢