Identity icon indicating copy to clipboard operation
Identity copied to clipboard

How would I change the primary keys in the identity tables to int instead of string?

Open rowanmiller opened this issue 10 years ago • 27 comments

Porting a question from the EF repo (https://github.com/aspnet/EntityFramework/issues/4879) asked by @TDK1964...

How would I change the primary keys in the identity tables to int instead of string?

I basically want to change all the string Guids PK's to int so this would include IdentityUserRole,IdentityUserLogin,IdentityUserClaim,IdentityRole,ApplicationUser (or should that be IdentityUser?)

I also want to change the table names to

builder.Entity<ApplicationUser>().ForSqlServerToTable().Table("Users");
builder.Entity<IdentityUserRole<string>>().ForSqlServerToTable("UserRoles");
builder.Entity<IdentityUserLogin<string>>().ForSqlServerToTable("UserLogins");
builder.Entity<IdentityUserClaim<string>>().ForSqlServerToTable("UserClaims");
builder.Entity<IdentityRole>().ForSqlServerToTable("Roles");

I have tried .ToTable("...") but this doesn't work either.

rowanmiller avatar Mar 22 '16 19:03 rowanmiller

It's doable but requires too much hassle that just decided to go with the default string ID. (You can not use IdentityDbContext, all the templates need to be changed)

This is a thing I really wish to be fixed soon too.

gdoron avatar Mar 22 '16 20:03 gdoron

Any thoughts?

TDK1964 avatar Mar 24 '16 12:03 TDK1964

Instructions for this would be quite important for us since we are using a large pre-existing database that uses INT as the primary user key.

ghost avatar Mar 29 '16 16:03 ghost

@rowanmiller who should we ping to get a response? It's sad that EF and asp.net core that give so much flexibility enforces such a thing that I believe most of the users wouldn't want. How many .Net applications are there that int isn't their pk type for users? You need an enormous amount of users to actually need to use GUID as the PK.

gdoron avatar Mar 29 '16 17:03 gdoron

@HaoK can you please respond to this question with details?

divega avatar Mar 29 '16 18:03 divega

Can you try adding classes for each like this:

    public class IntUser : IdentityUser<int>
    public class IntRole : IdentityRole<int>
    public class TestDbContext : IdentityDbContext<IntUser, IntRole, int>

HaoK avatar Mar 29 '16 18:03 HaoK

@HaoK It's not just the DbContext, it's Identity helper classes (e.g. UserManager) as well (and obviously the asp.net templates). Isn't it?

gdoron avatar Mar 29 '16 18:03 gdoron

There should be no change in UserManager nor in the templates, Identity itself always treats the key as a string. Its only the stores that store the keys whatever type you want (and to do the TKey => string translation)

HaoK avatar Mar 29 '16 18:03 HaoK

@HaoK Your solution worked for me. These are the changes I made and includes renaming the tables.

public class ApplicationUser : IdentityUser<int>
{
...
}
public class ApplicationRole : IdentityRole<int>
{
...
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
...
protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<ApplicationUser>(i => {
        i.ToTable("Users");
        i.HasKey(x => x.Id);
    });
    builder.Entity<ApplicationRole>(i => {
        i.ToTable("Role");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserRole<int>>(i => {
        i.ToTable("UserRole");
        i.HasKey(x => new { x.RoleId, x.UserId });
    });
    builder.Entity<IdentityUserLogin<int>>(i => {
        i.ToTable("UserLogin");
        i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
    });
    builder.Entity<IdentityRoleClaim<int>>(i => {
        i.ToTable("RoleClaims");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserClaim<int>>(i => {
        i.ToTable("UserClaims");
        i.HasKey(x => x.Id);
    });
    }
}

TDK1964 avatar Mar 30 '16 13:03 TDK1964

I'm doing some verification for the ASP.NET Identity features and have not been successful in getting the Identity DB to change the user ID type from guid to int. If this is going to be a common scenario, we may need better docs.

I tried doing what's described above by @TDK1964, but it ends up producing the error:

System.TypeLoadException
GenericArguments[0], 'WebApplication1.Data.ApplicationUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`8[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken]' violates the constraint of type parameter 'TUser'.

at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type)

at System.RuntimeTypeHandle.Instantiate(Type[] inst)

at System.RuntimeType.MakeGenericType(Type[] instantiation)

I don't know why this type error occurs at runtime, given that there was no build error. I'm using the 21343 (6/11) build.

So, is this a scenario we need to support, and if so, can you provide instructions for doing it? Have I missed where this is documented, or should I file an issue for it to be documented? Thanks!

SteveSandersonMS avatar Jun 16 '16 18:06 SteveSandersonMS

@HaoK @divega

blowdart avatar Jun 16 '16 18:06 blowdart

I'm on my mobile phone, but you should add to the startup class some code that says the PK is an int. I'll upload it soon. It works fine for me (using RC 2)

gdoron avatar Jun 16 '16 18:06 gdoron

I did also try changing the AddIdentity call in Startup.cs to specify the custom generic types:

services.AddIdentity<ApplicationUser, ApplicationRole>(...

... but that didn't change the outcome. Is there some other code I need to add?

SteveSandersonMS avatar Jun 16 '16 18:06 SteveSandersonMS

@SteveSandersonMS

        services.AddIdentity<ApplicationUser, Role>()
            .AddEntityFrameworkStores<ApplicationDbContext, int>()

This does the trick for me.

gdoron avatar Jun 16 '16 18:06 gdoron

Yeah that is an easy step to miss. We should make sure the docs are updated to make it clear.

HaoK avatar Jun 16 '16 18:06 HaoK

Thanks! Yes, that works :)

SteveSandersonMS avatar Jun 16 '16 19:06 SteveSandersonMS

@HaoK, BTW, why is it even needed? Why isn't inheriting from IdentityDbContext with the generic parameters <IntUser, IntRole, int> enough? It looks redundant.

gdoron avatar Jun 16 '16 21:06 gdoron

Because .AddEntityFrameworkStores<ApplicationDbContext>() is really .AddEntityFrameworkStores<ApplicationDbContext, string>()

At the end of the day, the primary key type is now a store specific thing, core identity always uses string for the keys.

HaoK avatar Jun 16 '16 21:06 HaoK

@HaoK I meant to say, that since you pass in ApplicationDbContext, you can infer the PK type from the IdentityDbContext generic parameter.

gdoron avatar Jun 16 '16 22:06 gdoron

Our constraint is that its a DbContext not necessarily one derived from IdentityDbContext

HaoK avatar Jun 16 '16 22:06 HaoK