尝试添加角色查询“ AspNetRoles”而不是“ AspNetUserRoles”

我正在研究一个项目,并且正在使用代码优先方法,并且正在使用ASP.NET Core 3.0和Entity Framework Core&Identity 3.0.0。我已经定义了实体,我想创建两个IdentityRoles,一个创建为Customer,另一个创建为Administrator。为此,我准备了以下方法:

  public static IServiceProvider ConfigureDefaultRoles(this IServiceProvider serviceProvider)
    {
        //TODO: figure out why the table is incorrectly named
        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<Guid>>>();
        var defaultUserRoles = new List<IdentityRole<Guid>>
        {
            new IdentityRole<Guid>() {Name = UserRoleDefinitions.CustomerRoleName},new IdentityRole<Guid>() {Name = UserRoleDefinitions.AdministratorRoleName}
        };

        foreach (var role in defaultUserRoles)
        {
            if (roleManager.RoleExistsAsync(role.Name).Result)
            {
                continue;
            }

            roleManager.CreateAsync(role);
        }

        return serviceProvider;
    }

Startup调用此方法,如下所示:

public void Configure(IApplicationBuilder app,IServiceProvider serviceProvider)
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
        app.ConfigureApplication();
        serviceProvider.ConfigureDefaultRoles();
    }

在启动时,这导致error之后。这里的主要问题是角色管理器正在尝试查询表AspNetRoles而不是AspNetUserRoles。我确保已应用迁移。我的数据库上下文如下:

 public class ApplicationDbContext : IdentityDbContext<User,IdentityRole<Guid>,Guid>,IDbContext
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="options">The options for the application context builder</param>
        public ApplicationDbContext(DbContextOptions options)
            : base(options)
        {
        }

        /// <summary>
        /// Apply the entity configurations defined in 'Configurations'.
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnmodelCreating(ModelBuilder modelBuilder)
        {
            // Call the base
            base.OnmodelCreating(modelBuilder);

            // Apply the configurations defined in 'Configurations'
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
        }
    }

此项目中没有实现任何角色,我正在使用IdentityRole<Guid>。身份配置如下:

public static IServiceCollection ConfigureIdentity(this IServiceCollection services)
{
    services
        .AddIdentity<User,IdentityRole<Guid>>(options => options.SignIn.RequireConfirmedaccount = true)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    return services;
}

我的User如下:User : IdentityUser<Guid>。此外,我查看了ApplicationDbContextModelsnapshot,发现实际上表AspNetRoles已声明为使用命名,错误提示缺少该错误,但该表是使用该名称重新构建的AspNetUserRoles。浏览数据库文件(app.db用于开发)仅显示AspNetUserRoles。完整快照可用here

    modelBuilder.Entity("microsoft.AspNetCore.Identity.IdentityRole<System.Guid>",b =>
                {
                    b.Property<Guid>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("TEXT");

                    b.Property<string>("ConcurrencyStamp")
                        .IsConcurrencyToken()
                        .HasColumnType("TEXT");

                    b.Property<string>("Name")
                        .HasColumnType("TEXT")
                        .HasMaxLength(256);

                    b.Property<string>("NormalizedName")
                        .HasColumnType("TEXT")
                        .HasMaxLength(256);

                    b.HasKey("Id");

                    b.HasIndex("NormalizedName")
                        .IsUnique()
                        .Hasname("RoleNameIndex");

                    b.ToTable("AspNetRoles");
                });


modelBuilder.Entity("microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>",b =>
                {
                    b.Property<Guid>("UserId")
                        .HasColumnType("TEXT");

                    b.Property<Guid>("RoleId")
                        .HasColumnType("TEXT");

                    b.HasKey("UserId","RoleId");

                    b.HasIndex("RoleId");

                    b.ToTable("AspNetUserRoles");
                });

很明显,我缺少了一点。

谢谢!

dhtz126 回答:尝试添加角色查询“ AspNetRoles”而不是“ AspNetUserRoles”

正如@IVan所说,两个表确实存在。问题是我有一个上下文生成器,它指向“持久性”项目中的数据源。但是,WebUI试图从其项目目录访问相同的数据源。这样就产生了一个没有任何表或迁移的新数据源。

我已设法通过切换到SQLServer(本地数据库)进行开发来解决此问题。

本文链接:https://www.f2er.com/3156277.html

大家都在问