226

我正在使用 Visual Studio 2013 的发布版本(RTM,而不是 RC)(从 MSDN 2013-10-18 下载),因此是 AspNet.Identity 的最新 (RTM) 版本。当我创建一个新的 Web 项目时,我选择“个人用户帐户”进行身份验证。这将创建以下表格:

  1. AspNet 角色
  2. AspNetUserClaims
  3. AspNetUserLogins
  4. AspNetUserRoles
  5. AspNet 用户

当我注册一个新用户(使用默认模板)时,这些表(上面列出的)被创建并且 AspNetUsers 表插入了一条记录,其中包含:

  1. ID
  2. 用户名
  3. 密码哈希
  4. 安全邮票
  5. 鉴别器

此外,通过向“ApplicationUser”类添加公共属性,我成功地向 AspNetUsers 表添加了其他字段,例如“FirstName”、“LastName”、“PhoneNumber”等。

这是我的问题。有没有办法更改上述表的名称(当它们第一次创建时)或者它们总是用AspNet我上面列出的前缀命名?如果表名可以不同的命名,请说明如何命名。

- 更新 -

我实施了@Hao Kung 的解决方案。它确实创建了一个新表(例如我称之为 MyUsers),但它仍然创建了 AspNetUsers 表。目标是用“MyUsers”表替换“AspNetUsers”表。请参阅下面的代码和创建的表的数据库图像。

我实际上想AspNet用我自己的名字替换每个表...对于 fxample、MyRoles、MyUserClaims、MyUserLogins、MyUserRoles 和 MyUsers。

我如何做到这一点并最终只得到一组表?

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string PhonePrimary { get; set; }
    public string PhoneSecondary { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(): base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("MyUsers");
    }
}

数据库表

-- 更新答案 --

感谢 Hao Kung 和 Peter Stulinski。这解决了我的问题...

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");
    }
4

7 回答 7

131

您可以通过修改 IdentityModel.cs 来轻松做到这一点,如下所示:

在 DbContext 中覆盖 OnModelCreating,然后添加以下内容,这会将 AspNetUser 表更改为“Users”,您还可以更改字段名称,默认 Id 列将变为 User_Id。

modelBuilder.Entity<IdentityUser>()
                    .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");

或者如果您想保留所有标准列名,则只需以下内容:

modelBuilder.Entity<IdentityUser>()
                        .ToTable("Users", "dbo")

下面的完整示例(这应该在您的 IdentityModel.cs 文件中)我将 ApplicationUser 类更改为 User。

public class User : IdentityUser
    {
        public string PasswordOld { get; set; }
        public DateTime DateCreated { get; set; }

        public bool Activated { get; set; }

        public bool UserRole { get; set; }

    }

public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
            modelBuilder.Entity<User>()
                .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
        }
    }

请注意,如果当前表存在,我还没有设法让它工作。另请注意,您未映射的任何列都将创建默认列。

希望有帮助。

于 2013-10-24T22:21:57.093 回答
65

以下是我的工作解决方案:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); // This needs to go before the other rules!

        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

有关更多详细信息,请参阅

于 2015-06-30T10:17:40.960 回答
15

您可以尝试在 DbContext 类中覆盖此方法以将其映射到您选择的表:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<IdentityUser>()
            .ToTable("AspNetUsers");
于 2013-10-19T00:06:24.153 回答
2

仅出于文档目的,对于那些在未来几年来这篇文章的人(像我一样 XD),放弃我的评论的所有答案都是正确的,但是您可以使用 Alexandru Bucur 在他的博客上给出的这种方法来简化

         //But this method is not longer supported on netcore > 2.2, so I need to fix it
         foreach (var entityType in modelBuilder.Model.GetEntityTypes())
         {
            var table = entityType.Relational().TableName;
             if (table.StartsWith("AspNet"))
             {
                 entityType.Relational().TableName = table.Substring(6);
             }
         };

        //This is the functional way on NetCore > 2.2
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            var tableName = entityType.GetTableName();
            if (tableName.StartsWith("AspNet"))
            {
                entityType.SetTableName(tableName.Substring(6));
            }
        }
于 2020-03-07T23:01:13.087 回答
1

我们可以像这样更改 asp.net Identity 默认表名:

    public class ApplicationDbContext : IdentityDbContext
    {    
        public ApplicationDbContext(): base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("user");
            modelBuilder.Entity<ApplicationUser>().ToTable("user");

            modelBuilder.Entity<IdentityRole>().ToTable("role");
            modelBuilder.Entity<IdentityUserRole>().ToTable("userrole");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("userclaim");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("userlogin");
        }
    }

此外,我们可以扩展每个类并向类添加任何属性,例如“IdentityUser”、“IdentityRole”……

    public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    public ApplicationRole() 
    {
        this.Id = Guid.NewGuid().ToString();
    }

    public ApplicationRole(string name)
        : this()
    {
        this.Name = name;
    }

    // Add any custom Role properties/code here
}


// Must be expressed in terms of our custom types:
public class ApplicationDbContext 
    : IdentityDbContext<ApplicationUser, ApplicationRole, 
    string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    // Add additional items here as needed
}

为了节省时间,我们可以使用AspNet Identity 2.0 可扩展项目模板来扩展所有类。

于 2016-03-01T11:46:39.923 回答
1

您还可以创建配置类并指定每个身份类的每个细节,例如:

using System.Data.Entity.ModelConfiguration;

public class ApplicationUserConfig : EntityTypeConfiguration<ApplicationUser>
{
    public UserConfig()
    {
        ToTable("Users");
        Property(u => u.LocationName).IsRequired();
    }
}

然后在 OnModelCreating() 方法中包含这些配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new ApplicationUserConfig());
        ...
    }

这将使您完全控制身份类的各个方面。

于 2017-01-16T16:50:08.547 回答
1

但它在 .NET CORE (MVC 6) 中不起作用,因为我们需要将绑定更改为

喜欢

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

    builder.Entity<IdentityRole>().ToTable("Role");
    builder.Entity<IdentityUser>(entity => 
    {
        entity.ToTable("User");
        entity.Property(p => p.Id).HasColumnName("UserId");
    });
}

它可能会帮助某人:)

于 2019-06-24T04:57:22.183 回答