ASP.NET vNext – Identity 3.0 (beta6) in MVC 6 (beta6) Spa – Using Integer Keys Instead of Strings

After looking all over the place and not finding anything specific for Identity 3.0, I got inspired to write this blog post on how to use bigint primary keys for the Identity tables instead of nvarchar(450). The official ASP .Net Identity web site has a good list of resources, but that are mostly 2.0 specific. Also, not much can be inferred from the tests on the aspnet/Identity project on Github,

There is already a blog post written by John Atten on “Extending Identity Models and Using Integer Keys Instead of Strings“, but it is targeted for ASP.NET Identity 2.0 and doesn’t work with ASP.NET Identity 3.0 beta6. Luckily, the steps to enable this scenario in Identity 3.0 are much easier and are finally being documented here.

First, extend the IdentityUser and IdentityRole and explicitly specify a type of long instead of string. For context, if you look at the definition of IdentityUser, you’ll see that it inherits from IdentityUser<string>.

public class ApplicationUser : IdentityUser<long>

public class ApplicationRole : IdentityRole<long>

Next, update your db context to be aware of the user/role and unqiue key changes.

public class ApplicationDbContext: 
    IdentityDbContext<ApplicationUser, ApplicationRole, long>

Next step, in Startup.cs file, ConfigureServices method, make sure the AddIdentity method points to the new user and role types. In addition, make sure the AddEntityFrameworkStores method specifies a long type.

services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext, long>()

At last, in your AccountController, make sure UserManagerSignInManager and RoleManager are bound to the right types.

UserManager<ApplicationUser> userManager;
SignInManager<ApplicationUser> signInManager;
RoleManager<ApplicationRole> rolesManager;

That’s it. If you look at this and the steps you had to follow in Identity 2.0, you’ll be able to easily tell how much the Identity framework architecture has improved in 3.0.



6 thoughts on “ASP.NET vNext – Identity 3.0 (beta6) in MVC 6 (beta6) Spa – Using Integer Keys Instead of Strings

  1. Hello
    Thanks for the great post is help me a lot.
    I just wont to comment that i did everything the same as in the post and when i try to create the migration it was failing with error:
    “The entity type ‘Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin’ requires a key to be defined.”
    Solution is:
    to add base.OnModelCreating(modelBuilder);
    inside the
    protected override void OnModelCreating(ModelBuilder modelBuilder) method inside Context
    Thanks one more time.

  2. Hi,

    Thank you for the post as I didn’t find any other blog talking about Identity 3 and how we can change it from string to long.

    I tried your solution but it is not working as it says something like:

    The object ‘PK_ApplicationUser’ is dependent on column ‘Id’. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.

    Your suggestion in this regard will be really appreciated.


  3. Hi,

    I figured out that it was cause of the Constraints which Entity framework is not able to update. Create the same tables with the Id as Int and it should solve your problem.

    Thanks Ali (author of this blog) for giving this lead.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s