简体   繁体   English

ASP.NET 核心 - 错误 CS0246 找不到类型或命名空间名称“ApplicationUserRole<>”

[英]ASP.NET Core - Error CS0246 The type or namespace name 'ApplicationUserRole<>' could not be found

In ASP.NET Core-6, I am trying to pull out all my Identity users and their associated roles for a user management.在 ASP.NET Core-6 中,我试图提取我的所有身份用户及其相关角色以进行用户管理。

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我对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?)错误 CS0246 找不到类型或命名空间名称“ApplicationUserRole<>”(您是否缺少 using 指令或程序集引用?)

Then, <ApplicationUserRole> is highlighted in ApplicationUserRoleConfigurations然后, <ApplicationUserRole>ApplicationUserRoleConfigurations中突出显示

How do I get this resolved?我该如何解决这个问题?

Thanks谢谢

as the error says, are you missing a using directive?正如错误所说,您是否缺少 using 指令? if you think you are not - then prove by showing namespaces of the files, specifically ApplicationRole.cs and ApplicationUserRoleConfigurations.cs .如果您认为自己不是- 然后通过显示文件的命名空间来证明,特别是ApplicationRole.csApplicationUserRoleConfigurations.cs Very likely that they are in different namespaces!他们很可能在不同的命名空间中!

Try to use:尝试使用:

public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole>

Add the using for the Core.Infrastructure.Identity to the class fileCore.Infrastructure.Identity的使用添加到 class 文件

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>>
    {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与ASP.NET和MVC 5一起出现错误...错误CS0246:找不到类型或命名空间 - error following along with ASP.NET and MVC 5… error CS0246: The type or namespace could not be found 错误 CS0246:找不到类型或命名空间名称“IApplicationBuilderExtensions” - error CS0246: The type or namespace name 'IApplicationBuilderExtensions' could not be found 错误 CS0246:找不到类型或命名空间名称“PlayerMotor” - error CS0246: The type or namespace name 'PlayerMotor' could not be found 错误CS0246:找不到类型或名称空间名称“ HtmlAgilityPack” - Error CS0246: The type or namespace name `HtmlAgilityPack' could not be found 错误CS0246:找不到类型或名称空间名称&#39;WebDriverWait&#39;? - Error CS0246: The type or namespace name 'WebDriverWait' could not be found? 错误 CS0246:找不到类型或命名空间名称“MySql” - error CS0246: The type or namespace name 'MySql' could not be found 错误 CS0246:找不到类型或命名空间名称“KustoConnectionStringBuilder” - error CS0246: The type or namespace name 'KustoConnectionStringBuilder' could not be found 错误CS0246:找不到类型或命名空间名称“AssemblyFileVersion” - Error CS0246: The type or namespace name 'AssemblyFileVersion' could not be found 错误 CS0246:找不到类型或命名空间名称“Newtonsoft” - Error CS0246: The type or namespace name 'Newtonsoft' could not be found 错误 CS0246:找不到类型或命名空间名称“CsvHelper” - error CS0246: The type or namespace name 'CsvHelper' could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM