Ayobamilaye
Ayobamilaye

Reputation: 1573

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

In ASP.NET Core-6 Web API, I am trying to implement IdentityDBContext.

I have these models:

public class ApplicationUser : IdentityUser<long>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MobileNumber { get; set; }

    [DefaultValue(false)]
    public bool? IsAdmin { get; set; }
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

public class ApplicationRole : IdentityRole<long>
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

public class ApplicationUserRole : IdentityUserRole<long>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

I separated the context for both Identity and the application DB Contexts:

For the Identity I have:

public class ApplicationIdentityDbContext : ApiAuthorizationDbContext<ApplicationUser>
{
    public ApplicationIdentityDbContext(DbContextOptions<ApplicationIdentityDbContext> options,
        IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {
    }
}

Then for the application, I have:

Then in the ApplicationDbContext, I have this:

public interface IApplicationDbContext
{
    public DbSet<RefreshToken> RefreshTokens { get; set; }
}

I customized the Identity keys from string to long.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long, IdentityUserClaim<long>, ApplicationUserRole, IdentityUserLogin<long>, IdentityRoleClaim<long>, IdentityUserToken<long>>, IApplicationDbContext
{
    public DbSet<RefreshToken> RefreshTokens { get; set; }

    public ApplicationDbContext(DbContextOptions<DDMDbContext> options)
        : base(options)
    {
    }
}

I got this error in the ApplicationIdentityDbContext:

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

How do I correct it?

Thank you.

Upvotes: 0

Views: 1112

Answers (2)

fared
fared

Reputation: 170

Hope this help someone as I faced the same thing.

My problem lied on namespace, I change the namespace on my IdentityModel.cs

I had namespace MyProject

and added a subname to the namespace namespace MyProject.Sub

This cause the error mentioned above, I just undo my namespace

Upvotes: 0

MR Alam
MR Alam

Reputation: 141

i think you should define your IdentityUserRole to your IdentityUser and IdentityRole . i mean define ApplicationUserRole for ApplicationUser and role change your classes to something like :

  public class ApplicationUser : IdentityUser<long , IdentityUserLogin,
ApplicationUserRole ,IdentityUserClaim>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MobileNumber { get; set; }
    
        [DefaultValue(false)]
        public bool? IsAdmin { get; set; }
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
    
    public class ApplicationRole : IdentityRole<long,ApplicationUserRole >
    {
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
    
    public class ApplicationUserRole : IdentityUserRole<long>
    {
        public virtual ApplicationUser User { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }

Upvotes: 1

Related Questions