繁体   English   中英

如何在ASP .NET Core 2.1中为从IdentityUser继承的类重命名AspNetUsers表?

[英]How to Rename AspNetUsers table for a class inherited from IdentityUser in ASP .NET Core 2.1?

我在使用PersonalAuthentication的项目中使用ASP .NET Core 2.1。 我的User表需要额外的属性,所以我继承了IdentityUser如下所示:

 public class ApplicationUser : IdentityUser
{
    [Required]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public string LastName { get; set; }
}

此修改后, AspNetUsers表未重命名。 重命名所有其他标识表。 我不知道为什么会这样。

在创建ApplicationUser类之后,我已经在我的Startup.cs代码中用ApplicationUser替换了IdentityUser

以下是修改前的代码

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

修改后

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

这是我的OnModelCreating方法

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        base.OnModelCreating(builder);

        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        //modelBuilder.Entity<IdentityUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim<string>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin<string>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>().ToTable("RoleClaim");
        modelBuilder.Entity<IdentityUserToken<string>().ToTable("UserToken");
    }

现在我不知道在重命名AspNetUsers表时我还缺少什么。 我没有找到任何解决方案,仍然在寻找。

流畅的配置是可以的,但用作上下文基础的标识类不是。

正如定制模型中所解释的那样(重点是我的):

自定义模型的起点是从适当的上下文类型派生 ; 见上一节。

前面的部分解释了基类,泛型类型参数和默认配置。

话虽如此,因为您只使用自定义的IdentityUser派生类,所以基本至少应该是IdentityDbContext<TUser>

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM