Alaa Alweish
Alaa Alweish

Reputation: 9084

Rename the default tables in asp.net identity 3 (dbo.ASPNetxxx) with Uniqueidentifier or int for IDs

I have created a new web application in ASP.Net Core 1.1, the default templates of ASP.Net identity 3 comes with nvarchar(400) or strings as the default data type for the "Id" fields.

I have changed the default TKey type from nvarchar(400) to Uniqueidentifier , Is there any way to change the default dbo.ASPNetxxx tables in asp.net identity to a custom name using the default code first approach?

Upvotes: 0

Views: 416

Answers (1)

Alaa Alweish
Alaa Alweish

Reputation: 9084

I have found the answer in a comment here and i would like to share it more organized here, Thanks to "Raffaele Garofalo" for answering this, I have copied the answer and added my 2 cents:

to rename the default tables in asp.net identity 3 and changing the data type to Uniqueidentifier, use the following steps:

In ApplicationDbContext.cs:

Change the ApplicationDbContext or whatever you used to reflect this one:

Change the class to be:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>

and the OnModelCreating method to be:

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

    // Customizations
    builder.Entity<ApplicationUser>(i =>
    {
        i.ToTable("Users");
        i.HasKey(x => x.Id);
    });
    builder.Entity<ApplicationRole>(i =>
    {
        i.ToTable("Roles");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserRole<Guid>>(i =>
    {
        i.ToTable("UserRoles");
        i.HasKey(x => new { x.RoleId, x.UserId });
    });
    builder.Entity<IdentityUserLogin<Guid>>(i =>
    {
        i.ToTable("UserLogins");
        i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
    });
    builder.Entity<IdentityRoleClaim<Guid>>(i =>
    {
        i.ToTable("RoleClaims");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserClaim<Guid>>(i =>
    {
        i.ToTable("UserClaims");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserToken<Guid>>(i =>
    {
        i.ToTable("UserTokens");
        i.HasKey(x => x.UserId);
    });

}

To add a default Guid value for Users and Roles IDs:

        builder.Entity<ApplicationUser>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("NEWID()");
        });

        builder.Entity<ApplicationRole>(b =>
        {
            b.Property(u => u.Id).HasDefaultValueSql("NEWID()");
        });

I have just removed the ASPNet extension from the names, you may add your custom table names. and you may replace Guid with int for integer IDs

In ApplicationUser.cs i have created a Custom ApplicationUser class

public class ApplicationUser : IdentityUser<Guid>
{
}

and a Custom ApplicationRole class

public class ApplicationRole : IdentityRole<Guid>
{
}

In Startup.cs under ConfigureServices method

Change your ASP.NET Core Startup according:

services
    .AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));


services
        .AddIdentity<ApplicationUser, ApplicationRole>()
        .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
        .AddDefaultTokenProviders();

Delete the content inside the Data/Migrations folder, but keep the Migrations folder there.

Now in the commands line (CMD), change the directory to the application root:

CD X:\ApplicationPathHere

If exist, Drop the existing DB because the schema can't be migrated from PK String to PK Guid

dotnet ef database drop -c ApplicationDbContext

Create the initial migration

dotnet ef migrations add InitialCreate -c ApplicationDbContext

Create the database

dotnet ef database update

Update For .Net Core 2.0 MVC project template,

You may get some errors in the ManageController, Just cast the Ids by adding .ToString()

and no need to pass int or GUID for:

.AddEntityFrameworkStores<ApplicationDbContext, Guid>()

just add

.AddEntityFrameworkStores<ApplicationDbContext>()

Upvotes: 1

Related Questions