使用 ASP.NET Identity 时如何更改表名?

使用 ASP.NET Identity 时如何更改表名?

问题描述:

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

I am using the release version (RTM, not RC) of Visual Studio 2013 (downloaded from MSDN 2013-10-18) and therefore the latest (RTM) version of AspNet.Identity. When I create a new web project, I select "Individual User Accounts" for authentication. This creates the following tables:

  1. AspNetRoles
  2. AspNetUserClaims
  3. AspNetUserLogins
  4. AspNetUserRoles
  5. AspNetUsers

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

When I register a new user (using the default template), these tables (listed above) are created and the AspNetUsers table has a record inserted which contains:

  1. 身份证
  2. 用户名
  3. 密码哈希
  4. SecurityStamp
  5. 鉴别器

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

Additionally, by adding public properties to the class "ApplicationUser" I have successfully added additional fields to the AspNetUsers table, such as "FirstName", "LastName", "PhoneNumber", etc.

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

Here's my question. Is there a way to change the names of the above tables (when they are first created) or will they always be named with the AspNet prefix as I listed above? If the table names can be named differently, please explain how.

-- 更新 --

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

I implemented @Hao Kung's solution. It does create a new table (for example I called it MyUsers), but it also still creates the AspNetUsers table. The goal is to replace the "AspNetUsers" table with the "MyUsers" table. See code below and database image of tables created.

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

I would actually like to replace each AspNet table with my own name... For fxample, MyRoles, MyUserClaims, MyUserLogins, MyUserRoles, and MyUsers.

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

How do I accomplish this and end up with only one set of tables?

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.这解决了我的问题...

Thanks to both Hao Kung and Peter Stulinski. This solved my problem...

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

您可以通过按照以下方式修改 IdentityModel.cs 来轻松完成此操作:

You can do this easily by modifying the IdentityModel.cs as per the below:

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

Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.

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

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

or simply the below if you want to keep all the standard column names:

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

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

Full example below (this should be in your IdentityModel.cs file) i changed my ApplicationUser class to be called 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");
        }
    }

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

Please note i have not managed to get this working if the current table exists. Also note whatever columns you do not map the default ones will be created.

希望有所帮助.