The type ApplicationUser cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser>'
Asked Answered
J

6

19

Trying to implement Identity in ASP.NET Core 2.0. Having many problems getting my head around this.

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("Ctrack6_Custom"))
        );


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

etc...

ApplicationUser.cs Using Guid for key. Also set in Role, etc

// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
    [MaxLength(50)]
    public string FirstName { get; set; }

    [MaxLength(50)]
    public string LastName { get; set; }

    [MaxLength(5)]
    public string OrgCode { get; set; }

    public ApplicationUser() : base()
    {

    }

}

ApplicationDbContext.cs Error is thrown in this file on the class definition. ApplicationDbContext is throwing this error:

The type 'App.Identity.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext'. There is no implicit reference conversion from 'App.Identity.ApplicationUser' to 'Microsoft.AspNetCore.Identity.IdentityUser'.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    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);
        builder.Entity<blah>()
        .HasKey(c => new { fields, for, key });

    }

    public DbSet<etc> Etcs {get; set; }

}
Jonme answered 12/4, 2018 at 5:43 Comment(0)
F
33

Change public class ApplicationDbContext : IdentityDbContext<ApplicationUser> to

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

You will need to have an ApplicationRole class similar to you ApplicationUser class. From what I remember once you specify the key type (in this case Guid, the default being string) you need to include the role and the key type even if you are not using roles.

Flank answered 12/4, 2018 at 13:25 Comment(3)
I don't know how I was meant to get from that error to that solution :/ Thank you very much :)Jonme
When inheriting from IdentityContext, we must keep all arguments in the expected order. That means you cannot change their positions! So, for a NET Core, we must inherit types and keep arguments as defined in the desired abstract class github.com/aspnet/Identity/blob/…Hooke
Or use IdentityRole<Guid> as mentioned in the answer below https://mcmap.net/q/632037/-the-type-applicationuser-cannot-be-used-as-type-parameter-39-tuser-39-in-the-generic-type-or-method-39-identitydbcontext-lt-tuser-gt-39Millhon
S
4

This is what working for me:

public class DataContext : IdentityDbContext<ApplicationUser,IdentityRole<Guid>,Guid>
Sanbenito answered 2/5, 2022 at 23:5 Comment(0)
H
3

i had this issue and finally realized that it was for my stupid mistake. make sure that the IdentityUser is derived from the package Microsoft.AspNetCore.Identity not Microsoft.AspNet.Identity.Core

Heedless answered 4/4, 2022 at 12:1 Comment(0)
B
3

I got this same error by accidentally deleting the inheritance in the class definition of the ApplicationUser:

public class ApplicationUser : IdentityUser
{
    
    ...
}
Bendwise answered 21/12, 2022 at 8:39 Comment(2)
Please, take a look to how to write an answerPulmotor
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Pulmotor
F
1

I have changed my package name from: Microsoft.AspNet.Identity to: Microsoft.AspNetCore.Identity and it worked.

Foresail answered 23/1 at 10:31 Comment(1)
Microsoft.AspNet.Identity is deprecated.Ladylike
A
1

For me I used using "Microsoft.AspNetCore.Identity" in ApplicationUser class, I changed it to "Microsoft.AspNetCore.Identity.EntityFrameworkCore"

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace Server.Models
{
    public class ApplicationUser : IdentityUser<Guid>
    { 
    }    
}
Agnostic answered 4/3 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.