david
david

Reputation: 11

Change Identity Id from string to int

I tried to change the Id from string to int but I encountered a problem : All the other posts have used in _Startup file :

    services.AddIdentity<User, IdentityRole<int>>.()
        .AddEntityFrameworkStores<DatabaseContext,int>()
        .AddDefaultTokenProviders();   

but when I try to do the same I get this error when I add int after DatabaseContext :

'IdentityBuilder' does not contain a definition for 'AddEntityFrameworkStores' and no extension method 'AddEntityFrameworkStores' accepting a first argument of type 'IdentityBuilder' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Presentation)

The other changes that I did :

       public class User : IdentityUser <int>  

       public class DatabaseContext :IdentityDbContext <User,IdentityRole<int>,int>                     

       protected override void OnModelCreating(ModelBuilder modelBuilder)

       {

        modelBuilder.Entity<User>(i => {
            i.ToTable("Users");
            i.HasKey(x => x.Id);
        });
        modelBuilder.Entity<IdentityRole>(i => {
            i.ToTable("Role");
            i.HasKey(x => x.Id);
        });
        modelBuilder.Entity<IdentityUserRole<int>>(i => {
            i.ToTable("UserRole");
            i.HasKey(x => new { x.RoleId, x.UserId });
        });
        modelBuilder.Entity<IdentityUserLogin<int>>(i => {
            i.ToTable("UserLogin");
            i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
        });
        modelBuilder.Entity<IdentityRoleClaim<int>>(i => {
            i.ToTable("RoleClaims");
            i.HasKey(x => x.Id);
        });
        modelBuilder.Entity<IdentityUserClaim<int>>(i => {
            i.ToTable("UserClaims");
            i.HasKey(x => x.Id);
        });
            base.OnModelCreating(modelBuilder);

} 

Upvotes: 1

Views: 624

Answers (1)

sramekpete
sramekpete

Reputation: 3208

Probably, you are using ASP.NET Core 2.x which is no longer accepting generic TKey argument.

The AddEntityFrameworkStores method doesn't accept a TKey argument as it did in ASP.NET Core 1.x. The primary key's data type is inferred by analyzing the DbContext object.

Related links:

Configure the ASP.NET Core Identity primary key data type

Upvotes: 1

Related Questions