Problem
I’m using Visual Studio 2013 release (RTM, not RC) (downloaded from MSDN 2013-10-18) and thus the newest (RTM) version of AspNet.Identity. I choose “Individual User Accounts” for authentication when I establish a new web project. The following tables are created as a result of this:
These tables (mentioned above) are created when I register a new user (using the default template), and a record is entered into the AspNetUsers table that contains:
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.
My question is as follows: Is it possible to modify the names of the aforementioned tables (when they are first formed) or will they always have the AspNet prefix as listed above? If the table names may be changed, please describe how.
— UPDATE —
@Hao Kung’s solution was implemented. Although it generates a new table (for example, MyUsers), it also builds the AspNetUsers table. The purpose is to use the “MyUsers” table to replace the “AspNetUsers” table. See the code and database image of the tables that were generated below.
I’d like to rename each AspNet table to something I can remember… MyRoles, MyUserClaims, MyUserLogins, MyUserRoles, and MyUsers, for example.
How can I accomplish this while only having 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");
}
}
— AN UPDATED RESPONSE —
Hao Kung and Peter Stulinski deserve special thanks. This was the solution to my dilemma…
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");
}
Asked by user2315985
Solution #1
You can simply accomplish this by making the following changes to IdentityModel.cs:
OnModel can be overridden. Adding the following to your DbContext will convert the AspNetUser table to “Users.” You may also alter the field names, with the default Id column becoming User Id.
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
Alternatively, if you wish to preserve all of the usual column names, just use the following:
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo")
Here’s a whole example (this should be in your IdentityModel.cs file) My ApplicationUser class has been renamed 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 that if the current table exists, I have not been able to get this to function. Also keep in mind that if you don’t map any columns, the default ones will be produced.
Hope that helps.
Answered by Piotr Stulinski
Solution #2
Here’s what I came up with:
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();
}
}
More information can be found here.
Answered by Frank Myat Thu
Solution #3
To map it to a table of your choice, try overriding this function in your DbContext class:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers");
Answered by Hao Kung
Solution #4
All of the solutions given in my reply are correct, but you may simplify things using this method supplied by Alexandru Bucur on his blog for others who visit to this page in the years and years to come (like me XD).
//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));
}
}
Answered by sgrysoft
Solution #5
We can change the default table names in asp.net Identity in the following way:
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");
}
}
We can also expand each class by adding any property to classes like ‘IdentityUser,’ ‘IdentityRole,’ and so on.
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
}
To save time, we can extend all of the classes using the AspNet Identity 2.0 Extensible Project Template.
Answered by Arvand
Post is based on https://stackoverflow.com/questions/19460386/how-can-i-change-the-table-names-when-using-asp-net-identity