Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change default ASP.NET Identity table names in .NET CORE?

I've started with .NET Core, in MVC 5 I changed default table names for example: AspNETUsers to Users in this way and it worked perfectly: In IdentityModels Class I dded:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<ApplicationUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }

But it does not work in ASP.NET CORE (MVC 6). Can Anyone help me? Thanks a lot.

like image 216
Archil Labadze Avatar asked Jan 03 '17 11:01

Archil Labadze


People also ask

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.

What is AspNetUserLogins table?

What is the AspNetUserLogins for? In Asp.net Identity, the Identity system uses the AspNetUserLogins table to hold information about 3rd party/external logins, for example users who login into your site via Google, Facebook, Twitter etc.

What is identity in asp net core?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

What is AspNetUserClaims table?

AspNetUserClaims” table is holding claims assigned to a user. A claim is different from a role because a claim is a key-value pair. You can have a role or not have a role. Claim also provides a value for a specified claim. In a way, it is like an optional property assigned to a user.


3 Answers

-To change the names of those tables ( IdentityUserRole<Tkey>, IdentityUserToken<Tkey>, IdentityRoleClaim<Tkey>, IdentityUserClaim<Tkey>, IdentityUserLogin<Tkey>) you have to specify the type of TKey(The type used for the primary key ) which is string(GUID) by default even if you didn't change it.

-If you want to change the primary key from GUID to int https://medium.com/@goodealsnow/asp-net-core-identity-3-0-6018fc151b4

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>(entity =>
        {
            entity.ToTable(name: "User");         
        });

        builder.Entity<IdentityRole>(entity =>
        {
            entity.ToTable(name: "Role");
        });
        builder.Entity<IdentityUserRole<string>>(entity =>
        {
            entity.ToTable("UserRoles");
          //in case you chagned the TKey type
          //  entity.HasKey(key => new { key.UserId, key.RoleId });
        });

        builder.Entity<IdentityUserClaim<string>>(entity =>
        {
            entity.ToTable("UserClaims");
        });

        builder.Entity<IdentityUserLogin<string>>(entity =>
        {
            entity.ToTable("UserLogins");
             //in case you chagned the TKey type
          //  entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });       
 });

        builder.Entity<IdentityRoleClaim<string>>(entity =>
        {
            entity.ToTable("RoleClaims");

        });

        builder.Entity<IdentityUserToken<string>>(entity =>
        {
            entity.ToTable("UserTokens");
            //in case you chagned the TKey type
           // entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name });

        });
    }
like image 157
Ahmed Al Jabry Avatar answered Oct 03 '22 20:10

Ahmed Al Jabry


Try to change binding to

builder.Entity<ApplicationUser>(entity =>
       {
           entity.ToTable(name:"Users");
           entity.Property(e => e.Id).HasColumnName("UserId");

       });
like image 20
Set Avatar answered Oct 03 '22 20:10

Set


A complete list for ASP.Net Core 2/2.1, based on @ahmed-al-jabry 's answer.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // Override default AspNet Identity table names
    modelBuilder.Entity<User>(entity => { entity.ToTable(name: "Users"); });
    modelBuilder.Entity<IdentityRole>(entity => { entity.ToTable(name: "Roles"); });
    modelBuilder.Entity<IdentityUserRole<string>>(entity => { entity.ToTable("UserRoles"); });
    modelBuilder.Entity<IdentityUserClaim<string>>(entity => { entity.ToTable("UserClaims"); });
    modelBuilder.Entity<IdentityUserLogin<string>>(entity => { entity.ToTable("UserLogins"); });
    modelBuilder.Entity<IdentityUserToken<string>>(entity => { entity.ToTable("UserTokens"); });
    modelBuilder.Entity<IdentityRoleClaim<string>>(entity => { entity.ToTable("RoleClaims"); });
}
like image 14
Andriod Avatar answered Oct 04 '22 20:10

Andriod