0

I am new to ASP.NET Core 6.0 Identity. I am planning to achieve the following:

  1. Make the default ID field to be of type int instead of GUID in the AspNetUsers table.
  2. I want to rename this field from ID to CompanyID.
  3. I want to rename the table from AspNetUsers to CompanyUsers.

After several Google searches, I couldn't find proper suggestions for all three of my requirements. I have already created the default AspNetUsers table with additional custom fields and it was working fine.

Now the requirement changed and several thousand rows from a legacy table will be moved to this table, and that's why I had to make these adjustments.

1 Answer 1

0

Here is the detailed steps for you. In my sample, I am using DBContext and MVCUser.

1. MVCUser, add <int> like below.

public class MVCUser : IdentityUser<int>
{
}

2. DBContext

using ASPNETCore6MVC.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace ASPNETCore6MVC.Areas.Identity.Data;

public class DBContext : IdentityDbContext<MVCUser, IdentityRole<int>, int>
{
    public DBContext(DbContextOptions<DBContext> options)
        : base(options)
    {
    }

    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);
        
        // Rename Table
        builder.Entity<MVCUser>(entity =>
        {
            entity.ToTable("CompanyUsers");
            entity.Property(e => e.Id).HasColumnName("CompanyID");
        });

        builder.Entity<IdentityRole<int>>(entity => entity.ToTable("Roles"));
        builder.Entity<IdentityUserRole<int>>(entity => entity.ToTable("UserRoles"));
        builder.Entity<IdentityUserClaim<int>>(entity => entity.ToTable("UserClaims"));
        builder.Entity<IdentityUserLogin<int>>(entity => entity.ToTable("UserLogins"));
        builder.Entity<IdentityUserToken<int>>(entity => entity.ToTable("UserTokens"));
        builder.Entity<IdentityRoleClaim<int>>(entity => entity.ToTable("RoleClaims"));
    }
}

3. Program.cs

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<DBContext>(options =>
            options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

//builder.Services.AddDefaultIdentity<MVCUser>(options => options.SignIn.RequireConfirmedAccount = true)
//    .AddEntityFrameworkStores<DBContext>();

builder.Services.AddIdentity<MVCUser, IdentityRole<int>>().AddEntityFrameworkStores<DBContext>().AddDefaultTokenProviders();

4. Backup your current database, or change the dbname in connectionString.

If not doing this, we may face To change the IDENTITY property of a column, the column needs to be dropped and recreated error.

5. Then we can restore the data from backup db.

The steps 4 is necessary, if the table name and structure are changed and there is already a lot of data, the previous data will affect the migration, so it is best not to change the table structure during later migration.

Test Result

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.