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
