I need to set all default Ids to int, but I only implemented a new user class. I am using Microsoft.AspNetCore.Identity and not Microsoft.AspNet.Identity
my user is :
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
the DBAccessContext.cs is:
public class DBAccessContext : IdentityDbContext<User>
{
public DBAccessContext(DbContextOptions<DBAccessContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
builder.Entity<User>(entity => { entity.ToTable(name: "Users");});
builder.Entity<IdentityRole>(entity => { entity.ToTable(name: "Roles"); });
builder.Entity<IdentityUserRole<string>>(entity => { entity.ToTable("Roles"); });
builder.Entity<IdentityUserClaim<string>>(entity => { entity.ToTable("Claims"); });
builder.Entity<IdentityUserLogin<string>>(entity => { entity.ToTable("Logins"); });
builder.Entity<IdentityRoleClaim<string>>(entity => { entity.ToTable("RoleClaims"); });
builder.Entity<IdentityUserToken<string>>(entity => { entity.ToTable("UserTokens"); });
//Calling the Seeding
builder.ApplyConfiguration(new RoleConfiguration());
}
}
and finally the configuration is as follows:
public static void ConfigureIdentity(this IServiceCollection services)
{
var builder = services.AddIdentityCore<User>(q => { q.User.RequireUniqueEmail = true; });
builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), services);
builder.AddEntityFrameworkStores<DBAccessContext>().AddDefaultTokenProviders();
}
You can change your default Ids to int in this way.
User:
public class User : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
DBAccessContext :
public class DBAccessContext : IdentityDbContext<User, IdentityRole<int>, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
//....
}
If the database was created before the PK change, run Drop-Database
drop the database and delete your Migrations
folder
Then migration and update database.
About more details,you can see the doc:Change the primary key type.