Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; }

}
like image 204
Hecatonchires Avatar asked Apr 12 '18 05:04

Hecatonchires


People also ask

Can applicationuser be used as a type parameter in generic type?

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'.

Can the type'user'be used as a type parameter'tuser'?

The type 'User' cannot be used as type parameter 'TUser' in the generic type or method' IdentityDbContext<TUser>'.There is no implicit reference conversion from 'User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

Can tcontext be used as a parameter in a generic type?

CS0311 C# The type cannot be used as type parameter 'TContext' in the generic type or method. EntityFrameworkCore ~ Stack Overflow ~ AnswerBun.com

How to convert type1 to Type2 in Java?

There is no implicit reference conversion from 'type1' to 'type2'. When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. Change the argument you are using to create the class.


2 Answers

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.

like image 118
Kevin Avatar answered Oct 31 '22 05:10

Kevin


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

like image 20
AleM Avatar answered Oct 31 '22 06:10

AleM