How can I change the table names when using ASP.NET Identity?
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.
How can I change default ASP.NET Identity table names in .NET CORE?
Try to change binding to
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name:"Users");
entity.Property(e => e.Id).HasColumnName("UserId");
});
How to change table names in Asp.net Identity 3.0?
You can do this easily by changing the entity mapping with extension method ToTable("TableName")
on OnModelCreating
of your DbContext
:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().ToTable("Users"); // Your custom IdentityUser class
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityRole>().ToTable("Roles");
}
The only catch here is to remember to use the generics with the type of your identifier (string is default on AspNetCore.
How to customize table names for Asp.Net Identity (ex. change from AspNetUsers to a custom name)
You can add OnModelCreating
method in IdentityModel.cs
class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("CustomName");
}
}
Change column names of identity tables (Asp.Net)
In your onModelCreating Class, do the following:
modelBuilder.Entity<ApplicationUser>().Property(p => p.Id).HasColumnName("strUserID");
modelBuilder.Entity<ApplicationUser>().Property(p =>p.Email).HasColumnName("strUserEmail");
modelBuilder.Entity<ApplicationUser>().Property(p => p.EmailConfirmed).HasColumnName("strUserEmailConfirmed");
Trying to change table names in ASP.NET Identity 2.0
Few steps to follow:
- Install NuGet Package:
Microsoft.AspNet.Identity.EntityFramework
- Add a
connection string
to your web.config/app.config
Now you have to define your custom Database Context
:
public class MyContext : IdentityDbContext
{
public MyContext()
: base(<connection string name>)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<IdentityRole>()
.ToTable("Roles");
modelBuilder.Entity<IdentityUserRole>()
.ToTable("UserRoles");
modelBuilder.Entity<IdentityUserClaim>()
.ToTable("UserClaims");
modelBuilder.Entity<IdentityUserLogin>()
.ToTable("UserLogins");
}
}
As you can see I've used DbModelBuilder
to map all the entities to a new tables.
- Open NuGet Package Manager Console
- Execute command
Enable-Migrations
It will create a folder Migrations
with the configuration file Configuration.cs
.
It should look something like this:
internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApplication1.Models.MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(ConsoleApplication1.Models.MyContext context)
{
}
}
In the constructor change the property AutomaticMigrationsEnabled
to true
.
- Open NuGet Package Manager Console
- Execute command
Update-Database
It should run the script to create the new tables.
You can customize your entities (and Ids) creating custom class for each interface defined.
public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
}
Since you're using Owin you can define your UserStore:
public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyUserStore(MyContext context)
: base(context)
{
}
}
and your UserManager implementation:
public class ApplicationUserManager : UserManager<ASPNETIdentity2.Models.MyUser, string>
{
public ApplicationUserManager(IUserStore<ASPNETIdentity2.Models.MyUser, string> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>()));
manager.UserValidator = new UserValidator<MyUser, string>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 5,
RequireNonLetterOrDigit = false, // true
// RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
return (manager);
}
}
And your Owin.Startup
should look something like this:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(MyContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
}
If you want to have a look at a custom implementation you can check my GitHub repository with a simple working solution on ASP.NET MVC.
UPDATE:
There's another project in that solution with a minimal setup; basically you only need to define your context (IdentityDbContext) if you only want to rename your tables.
The project can be found here.
Related Topics
Using String Format to Show Decimal Up to 2 Places or Simple Integer
How to Determine the Owner of a Process in C#
Differencebetween a Shared Project and a Class Library in Visual Studio 2015
Code for Decoding/Encoding a Modified Base64 Url (In ASP.NET Framework)
Can't View Designer When Coding a Form in C#
Why File.Readalllinesasync() Blocks the UI Thread
Find Number of Decimal Places in Decimal Value Regardless of Culture
Window VS Page VS Usercontrol for Wpf Navigation
How to Intercept a Method Call in C#
Easiest Way to Parse JSON Response
How to Embed a Text File in a .Net Assembly
Effective Way to Find Any File's Encoding
How to Create a Dto in C# ASP.NET from a Fairly Complex JSON Response
Fire and Forget Async Method in ASP.NET MVC
Differencebetween Nullable<T>.Hasvalue or Nullable<T> != Null
System.Unauthorizedaccessexception While Running .Exe Under Program Files