find and solve || findandsolve.com
Please wait.....

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

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

Severity Code Project Description File Line Suppression State

Error CS0311 Repository The type 'Entities.Account.User' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>'. There is no implicit reference conversion from 'Entities.Account.User' to 'Microsoft.AspNetCore.Identity.IdentityUser<string>'. C:\Users\User-Pc\Desktop\AMS\Repository\DataContext.cs 8 Active

Severity Code Project Description File Line Suppression State

Error CS0311 Repository The type 'Entities.Account.Role' cannot be used as type parameter 'TRole' in the generic type or method 'IdentityDbContext<TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>'. There is no implicit reference conversion from 'Entities.Account.Role' to 'Microsoft.AspNetCore.Identity.IdentityRole<string>'. C:\Users\User-Pc\Desktop\AMS\Repository\DataContext.cs 8 Active

This is my code in .net 5

DataContext.cs

   public class DataContext : IdentityDbContext<User, Role, string, IdentityUserClaim<string>, IdentityUserRole<string>,
      IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>
    {        
        public DataContext(DbContextOptions<DataContext> options)
           : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //builder.Entity<SetupCategory>(x => { x.ToTable("SetupCategory"); });
        }
    }

In my User.cs class

    public class User : IdentityUser
    {
    }



Answer 

Option  1.

Replace User to IdentityUser and Role to IdentityRole and working fine.

   public class DataContext : IdentityDbContext<IdentityUser,IdentityRole, string, IdentityUserClaim<string>, IdentityUserRole<string>,
       IdentityUserLogin<string>, IdentityRoleClaim<string>,IdentityUserToken<string>>
      {
        //public DbSet<Organization> SetupCategory { get; set; }
        public DataContext(DbContextOptions<DataContext> options)
           : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //builder.Entity<SetupCategory>(x => { x.ToTable("SetupCategory"); });
        }
    }


Option 2.

public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class MyDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}


Option 3.

Change public class DataContext : IdentityDbContext<User> to

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

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 should include the role and the key type even if you are not using roles.


Option 4.

Solved this issue by the package that was keeping those classes.

Microsoft.AspNetCore.Identity.EntityFrameworkCore

To access those clases

(IdentityUser and IdentityRole) one must add

using Microsoft.AspNetCore.Identity;


Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response