简体   繁体   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?

I'm using ASP .NET Core 2.1 for my project with IndividualAuthentication. 我在使用PersonalAuthentication的项目中使用ASP .NET Core 2.1。 I need extra properties for my User table so i inherited from IdentityUser like below : 我的User表需要额外的属性,所以我继承了IdentityUser如下所示:

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

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

After this modification AspNetUsers table is not renamed. 此修改后, AspNetUsers表未重命名。 All other identity tables are renamed. 重命名所有其他标识表。 I don't know why is this happening. 我不知道为什么会这样。

After creating ApplicationUser class i have replaced IdentityUser with ApplicationUser in my code of Startup.cs 在创建ApplicationUser类之后,我已经在我的Startup.cs代码中用ApplicationUser替换了IdentityUser

Below is the code before Modification 以下是修改前的代码

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

After modification 修改后

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

This is my OnModelCreating method 这是我的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");
    }

Now i don't know what other thing i am missing for renaming AspNetUsers table. 现在我不知道在重命名AspNetUsers表时我还缺少什么。 I didn't find any solution and still searching. 我没有找到任何解决方案,仍然在寻找。

The fluent configuration is ok, but the identity class used as base for your context is not. 流畅的配置是可以的,但用作上下文基础的标识类不是。

As explained in the Customizing the model (emphasis is mine): 正如定制模型中所解释的那样(重点是我的):

The starting point for customizing the model is to derive from the appropriate context type ; 自定义模型的起点是从适当的上下文类型派生 ; see the preceding section. 见上一节。

and the preceding sections explain the base classes, generic type arguments and the default configuration. 前面的部分解释了基类,泛型类型参数和默认配置。

With that being said, since you are using only a custom IdentityUser derived class, the base at least should be the IdentityDbContext<TUser> : 话虽如此,因为您只使用自定义的IdentityUser派生类,所以基本至少应该是IdentityDbContext<TUser>

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

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

相关问题 Asp.net核心 - 没有这样的表:AspNetUsers - Asp.net core - no such table: AspNetUsers 在ASP.NET Core 2.1中更改IdentityUser类型 - Changing IdentityUser Type in ASP.NET Core 2.1 无法从ASP.NET Core 2.1控制器获取IdentityUser数据 - Unable to get IdentityUser data from ASP.NET Core 2.1 controller ASP.NET Core 2.1 - IdentityUser 问题 - 无法为“IdentityUser”创建 DbSet 此类型未包含在上下文模型中 - ASP.NET Core 2.1 - IdentityUser Issue - Cannot create a DbSet for 'IdentityUser' this type is not included in the model for the context AspNetUsers 到 IdentityUser 与 Entity Framework Core - AspNetUsers to IdentityUser with Entity Framework Core 是否将用户设置存储在ASP.NET Core身份AspNetUsers表中 - Store User Settings in ASP.NET Core Identity AspNetUsers Table or Not 如何有条件地将 map 多个模型添加到 ASP.NET 核心标识中的一张表(AspNetUsers)? - How to conditionally map multiple models to one table (AspNetUsers) in ASP.NET Core Identity? ASP.NET Core 2.1从类库引用ApplicationDbContext - ASP.NET Core 2.1 Referring to ApplicationDbContext from Class Library ASP.NET Core(2.1)中的身份:为什么我们需要添加新的Context来自定义IdentityUser? - Identity in ASP.NET Core (2.1): Why do we need to add a new Context to customize IdentityUser? ASP Net Core MVC:如何在partialview中获取aspnetusers列 - Asp Net Core MVC: How to get Column of aspnetusers in a partialview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM