本文介绍了我如何使用Visual Studio 2013 ASPNET身份时更改表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2013的发布版(RTM,而不是RC)(从MSDN下载2013年10月18日),因此最新的(RTM)AspNet.Identity版本。当我创建一个新的Web项目,我选择认证个人用户帐户。这将创建下列表格:


  1. AspNetRoles

  2. AspNetUserClaims

  3. AspNetUserLogins

  4. AspNetUserRoles

  5. AspNetUsers

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


  1. 标识

  2. 用户名

  3. PasswordHash

  4. SecurityStamp

  5. 鉴别

此外,通过增加公共属性的类ApplicationUser我已经成功添加附加字段到AspNetUsers表,如名字,姓氏,******中国等。

下面是我的问题。有没有办法来改变上述表的名称(当他们第一次创建),或者将它们始终与ASPNET preFIX命名为我上面列出?如果表名可以有不同的名称,请解释如何。谢谢 -

- 更新 -

我实现@Hao功夫的解决方案。它创建一个新表(例如我把它叫做MyUsers),但它也还是创建AspNetUsers表。我们的目标是与MyUsers表取代AspNetUsers表。请参见下面code和数据库映像创建的表。

我真的想用自己的名字来代替每个ASPNET表...例如:MyRoles,MyUserClaims,MyUserLogins,MyUserRoles,MyUsers

这是如何做到这一点,只有一组表结束任何想法?

 公共类ApplicationUser:IdentityUser
{
    公共字符串名字{获得;组; }
    公共字符串名字{获得;组; }
    公共字符串地址1 {搞定;组; }
    公共字符串地址2 {搞定;组; }
    公共字符串城{搞定;组; }
    公共字符串状态{搞定;组; }
    公共字符串邮政code {搞定;组; }
    公共字符串PhonePrimary {搞定;组; }
    公共字符串PhoneSecondary {搞定;组; }
}公共类ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
    公共ApplicationDbContext():基地(DefaultConnection)
    {
    }    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        base.OnModelCreating(模型构建器);
        modelBuilder.Entity< IdentityUser>()ToTable(MyUsers);
    }
}

- 更新应答 -

由于双方郝哭嗯和彼得Stulinski。这解决了我的问题...

 保护覆盖无效OnModelCreating(System.Data.Entity.DbModelBuilder模型构建器)
    {
        base.OnModelCreating(模型构建器);        modelBuilder.Entity< IdentityUser>()ToTable(MyUsers),性质(P => p.Id)。.HasColumnName(用户ID);
        modelBuilder.Entity< ApplicationUser>()ToTable(MyUsers),性质(P => p.Id)。.HasColumnName(用户ID);
        modelBuilder.Entity< IdentityUserRole>()ToTable(MyUserRoles);
        modelBuilder.Entity< IdentityUserLogin>()ToTable(MyUserLogins);
        modelBuilder.Entity< IdentityUserClaim>()ToTable(MyUserClaims);
        modelBuilder.Entity< IdentityRole>()ToTable(MyRoles);
    }


解决方案

您可以很容易地通过修改按以下的IdentityModel.cs做到这一点

覆盖OnModelCreating在你的DbContext然后添加以下,这将改变AspNetUser表用户您还可以更改字段名称默认Id列将成为USER_ID。

  modelBuilder.Entity< IdentityUser>()
                    .ToTable(用户,DBO),性质(P => p.Id)。.HasColumnName(USER_ID);

或只是如果你想保留的所有标准列名如下:

  modelBuilder.Entity< IdentityUser>()
                        .ToTable(用户,DBO)

以下(这应该是你的IdentityModel.cs文件)我改变了我的ApplicationUser类完整的示例被称为用户。

 公共类用户:IdentityUser
    {
        公共字符串PasswordOld {搞定;组; }
        公众的DateTime dateCreated会获得{;组; }        公共BOOL激活{搞定;组; }        公共BOOL的UserRole {搞定;组; }    }公共类ApplicationDbContext:IdentityDbContext<使用者>
    {
        公共ApplicationDbContext()
            :基地(DefaultConnection)
        {
        }        保护覆盖无效OnModelCreating(System.Data.Entity.DbModelBuilder模型构建器)
        {
            base.OnModelCreating(模型构建器);
            modelBuilder.Entity< IdentityUser>()
                .ToTable(用户,DBO),性质(P => p.Id)。.HasColumnName(USER_ID);
            modelBuilder.Entity<使用者>()
                .ToTable(用户,DBO),性质(P => p.Id)。.HasColumnName(USER_ID);
        }
    }

请注意我没有设法得到这个工作,如果当前的表存在。还要注意无论你列没有映射默认的值,将被创建。

希望有所帮助。

I am using the release version (RTM, not RC) of Visual Studio 2013 (downloaded from MSDN 10/18/2013) 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

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. Id
  2. UserName
  3. PasswordHash
  4. SecurityStamp
  5. Discriminator

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.

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. Thanks--.

-- UPDATE --

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.

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

Any ideas on how to 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");
    }
}

-- UPDATE ANSWER --

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");
    }
解决方案

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

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

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.

Hope that helps.

这篇关于我如何使用Visual Studio 2013 ASPNET身份时更改表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 06:30
查看更多