使用ASP.NET Identity时如何更改表名称? [英] How can I change the table names when using ASP.NET Identity?

查看:285
本文介绍了使用ASP.NET Identity时如何更改表名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2013的发行版(RTM,而不是RC)(从MSDN 2013-10-18下载),因此使用的是AspNet.Identity的最新(RTM)版本.创建新的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. PasswordHash
  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表.目标是将"AspNetUsers"表替换为"MyUsers"表.请参见下面的代码和所创建表的数据库图像.

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

-更新答案-

感谢郝孔和彼得·斯图林斯基(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.

希望有帮助.

这篇关于使用ASP.NET Identity时如何更改表名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆