【问题标题】:How to Rename AspNetUsers table for a class inherited from IdentityUser in ASP .NET Core 2.1?如何重命名从 ASP .NET Core 2.1 中的 IdentityUser 继承的类的 AspNetUsers 表?
【发布时间】:2018-12-29 12:46:20
【问题描述】:

我在我的项目中使用 ASP .NET Core 2.1 和 IndividualAuthentication。我的 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 的代码中将IdentityUser 替换为ApplicationUser

下面是修改前的代码

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 表还缺少什么。我没有找到任何解决方案,仍在寻找。

【问题讨论】:

  • 很可能您没有使用正确的基本泛型IdentityDbContext。我们能看到吗,例如class ApplicationDbContext : IdentityDbContext&lt;…&gt;?
  • public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options) { } }

标签: c# entity-framework-migrations asp.net-core-identity asp.net-core-2.1 ef-core-2.1


【解决方案1】:

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

正如Customizing the model 中所述(重点是我的):

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

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

话虽如此,由于您只使用自定义的IdentityUser 派生类,因此基类至少应该是IdentityDbContext&lt;TUser&gt;:

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

【讨论】:

猜你喜欢
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多