10

I'm trying to change the primary key in ASPNET Core 2.0 to Guid or long.

I've tried the following examples in both these links Link1 and Link2

However, in the Startup class I get the following error

 Error  CS1061  '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?)

enter image description here

I think the reason is the examples are based on the older ASPNET Core 1.0. How can I change the primary key for Identity in ASPNET Core 2.0?

  • I'm having the same problem. Did you find a solution to this? – devC Jan 07 '18 at 16:14
  • 3
    @devC I had to install [Microsoft.AspNetCore.Identity.EntityFrameworkCore](https://www.nuget.org/packages/Microsoft.AspNetCore.Identity.EntityFrameworkCore/) – SSlinky Feb 01 '21 at 03:42

5 Answers5

16

Credit on how to do this can be found in the comments in this link https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x

Change ApplicationUser to inherit from IdentityUser<int>

Create a new class ApplicationRole that inherits from IdentityRole<int>

Change ApplicationDbContext to inherit from IdentityDbContext<ApplicationUser, ApplicationRole, int>

Change startup.cs for services.AddIdentity to use ApplicationRole

Change UrlHelperExtensions methods to take a generic <T> with T userId in the signature

Change ManageController's LinkLoginCallback's call to

await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString())

Add the following line to the ApplicationDbContext OnModelCreating method (after the base.OnModelCreating call)

builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();

If using Guid then replace

builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn(); 

with

builder.Entity<ApplicationUser>().Property(p => p.Id).ValueGeneratedOnAdd();

All changes are below

public class ApplicationUser : IdentityUser<Guid>
{
}

public class ApplicationRole : IdentityRole<Guid>
{
    
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    Public ApplicationDbContext(DbContextOptions < ApplicationDbContext > 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);

        // For Guid Primary Key
        builder.Entity<ApplicationUser>().Property(p => p.Id).ValueGeneratedOnAdd();

        // For int Primary Key
        //builder.Entity<ApplicationUser>().Property(p => p.Id).UseSqlServerIdentityColumn();
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

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

    // Add application services.
    services.AddTransient<IEmailSender, EmailSender>();

    services.AddMvc();
}

public static class UrlHelperExtensions
{
    public static string EmailConfirmationLink<T>(this IUrlHelper urlHelper, T userId, string code, string scheme)
    {
        return urlHelper.Action(
            action: nameof(AccountController.ConfirmEmail),
            controller: "Account",
            values: new { userId, code },
            protocol: scheme);
    }

    public static string ResetPasswordCallbackLink<T>(this IUrlHelper urlHelper, T userId, string code, string scheme)
    {
        return urlHelper.Action(
            action: nameof(AccountController.ResetPassword),
            controller: "Account",
            values: new { userId, code },
            protocol: scheme);
    }
}

....
var info = await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString());
Lucas B
  • 69
  • 2
  • 7
  • i make the all changes like this, but i got the run time error can u look at this https://stackoverflow.com/questions/50579422/net-core-user-table-id-column-string-to-int-migration-and-runtime-error – LittleDragon May 29 '18 at 09:09
12

What fixed it for me was just adding 'Microsoft.AspNetCore.Identity.EntityFrameworkCore' via NuGet

If it's anything like a Similar issue I had when adding Identity to my Core 3.1 project then this may have been moved into a separate NuGet Package. (I updated my .NET CORE project from 2.1 to 3.1, yet I have some errors in the class Startup)

George
  • 121
  • 1
  • 3
3

I solved this by changing my ApplicationDbContext to inherit from IdentityDbContext<ApplicationUser>

Siphamandla Ngwenya
  • 2,696
  • 1
  • 16
  • 21
2

I have the same problem, after installing Microsoft.AspNetCore.Identity.EntityFrameworkCore from the Nuget Package Manager my error is resolved.

0

Add this nugget package ' <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore' to the project and rebuild

snnpro
  • 193
  • 2