Home > Back-end >  ASP.NET Core - Error CS0246 The type or namespace name 'ApplicationUserRole<>' could
ASP.NET Core - Error CS0246 The type or namespace name 'ApplicationUserRole<>' could

Time:08-01

In ASP.NET Core-6, I am trying to pull out all my Identity users and their associated roles for a user management.

ApplicationUser

using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
{
    public class ApplicationUser : IdentityUser
    {
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
}

ApplicationUserRole

using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
{
    public class ApplicationUserRole : IdentityUserRole<string>
    {
        public virtual ApplicationUser User { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }
}

ApplicationRole

using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
{
    public class ApplicationRole : IdentityRole
    {
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
}

ApplicationUserConfigurations

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Core.Persistence.Configurations
{
    public class ApplicationUserConfigurations : IEntityTypeConfiguration<ApplicationUser>
    {
        public void Configure(EntityTypeBuilder<ApplicationUser> builder)
        {
            builder.Property(u => u.IsActive).HasDefaultValue(true);
            builder.Property(u => u.IsAdmin).HasDefaultValue(false);
            builder.Property(u => u.IsPasswordChanged).HasDefaultValue(false);
            builder.Property(u => u.IsDeleted).HasDefaultValue(false);
        }
    }
}

ApplicationRoleConfigurations

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Core.Persistence.Configurations
{
    public class ApplicationRoleConfigurations : IEntityTypeConfiguration<ApplicationRole>
    {

        public void Configure(EntityTypeBuilder<ApplicationRole> builder)
        {
            builder.HasIndex(r => r.Name).IsUnique();
            builder.Property(r => r.IsActive).HasDefaultValue(true);
        }
    }
}

ApplicationUserRoleConfigurations

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole<string>>
{
    public void Configure(EntityTypeBuilder<ApplicationUserRole<string>> builder)
    {
        builder.HasKey(ur => new { ur.UserId, ur.RoleId });

        builder.HasOne(ur => ur.Role)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(ur => ur.RoleId)
            .IsRequired();

        builder.HasOne(ur => ur.User)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(ur => ur.UserId)
            .IsRequired();
    }
}

DBContext

public class ApplicationDbContext
    : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
    ApplicationUserRole, IdentityUserLogin<string>,
    IdentityRoleClaim<string>, IdentityUserToken<string>>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
            base.OnModelCreating(builder);

            // Customize ASP.NET Identity models and override defaults
            // such as renaming ASP.NET Identity, changing key types etc.
            builder.ApplyConfiguration(new ApplicationUserConfigurations());
            builder.ApplyConfiguration(new ApplicationUserRoleConfigurations());
            builder.ApplyConfiguration(new IdentityRoleClaimConfigurations());
            builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
            builder.ApplyConfiguration(new IdentityUserLoginConfigurations());
            builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
            builder.ApplyConfiguration(new ApplicationRoleConfigurations());
        }
}

I have issues with ApplicationUserRoleConfigurations

I got this error:

Error CS0246 The type or namespace name 'ApplicationUserRole<>' could not be found (are you missing a using directive or an assembly reference?)

Then, <ApplicationUserRole> is highlighted in ApplicationUserRoleConfigurations

How do I get this resolved?

Thanks

CodePudding user response:

as the error says, are you missing a using directive? if you think you are not - then prove by showing namespaces of the files, specifically ApplicationRole.cs and ApplicationUserRoleConfigurations.cs. Very likely that they are in different namespaces!

CodePudding user response:

Try to use:

public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole>

CodePudding user response:

Add the using for the Core.Infrastructure.Identity to the class file

using Core.Infrastructure.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole<string>>
{

OR put it in the same namespace

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Core.Infrastructure.Identity
{
    public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole<string>>
    {
  • Related