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

AspNetRoles AspNetUserClaims AspNetUserLogins AspNetUserRoles AspNetUsers

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

Id 用户名 PasswordHash SecurityStamp 鉴频器

此外,通过向类“ApplicationUser”添加公共属性,我已经成功地向AspNetUsers表添加了额外的字段,如“FirstName”,“LastName”,“PhoneNumber”等。

这是我的问题。是否有一种方法可以更改上述表的名称(当它们第一次创建时),或者它们总是像我上面列出的那样以AspNet前缀命名?如果表名可以以不同的方式命名,请解释如何命名。

——更新——

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

实际上,我想用我自己的名字替换每个AspNet表…例如,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");
    }
}

——更新答案——

多亏了郝坤和彼得·斯图林斯基。这解决了我的问题……

    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");
    }

当前回答

我们可以像这样更改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可扩展项目模板来扩展所有类。

其他回答

我们可以像这样更改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可扩展项目模板来扩展所有类。

以下是我的解决方案:

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();
    }
}

更多细节请参见此

只是为了文档的目的,对于那些在未来的几年里来到这篇文章的人(比如我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));
            }
        }

对于。net 6,你需要使用duend . identityserver . entityframework . entities

        protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>().ToTable("SecurityUsers");  
        modelBuilder.Entity<IdentityRole>().ToTable("SecurityRole");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("SecurityUserRole");
        modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("SecurityUserClaim");
        modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("SecurityUserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("SecurityRoleClaims");
        modelBuilder.Entity<IdentityUserToken<string>>().ToTable("SecurityUserTokens");

        modelBuilder.Entity<Duende.IdentityServer.EntityFramework.Entities.PersistedGrant>().ToTable("SecurityPersistedGrant");
        modelBuilder.Entity<Duende.IdentityServer.EntityFramework.Entities.Key>().ToTable("SecurityKey");
        modelBuilder.Entity<Duende.IdentityServer.EntityFramework.Entities.DeviceFlowCodes>().ToTable("SecurityDeviceCode");

    }

你可以很容易地通过修改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类被称为用户。

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");
        }
    }

请注意,我还没有设法让这个工作,如果当前表存在。还要注意,任何您没有映射默认列的列都会被创建。