尝试添加角色查询 'AspNetRoles' 而不是 'AspNetUserRoles'
Attempting to add a role queries 'AspNetRoles' instead of '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。这里的主要问题是角色管理器试图查询 table 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
并且我注意到确实 table AspNetRoles
被声明为使用命名错误抱怨丢失,但是 table 以 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");
});
显然,我漏掉了一点..
谢谢!
正如@IVan 所说,这两个表确实存在。问题是我有一个上下文生成器,它指向位于 'Persistence' 项目中的数据源。但是,WebUI 试图从其项目目录访问相同的数据源。这导致没有任何表或迁移的新数据源。
我已经通过切换到 SQLServer(本地数据库)进行开发来解决这个问题。
我正在做一个项目,我正在使用代码优先方法,我正在使用 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。这里的主要问题是角色管理器试图查询 table 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
并且我注意到确实 table AspNetRoles
被声明为使用命名错误抱怨丢失,但是 table 以 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");
});
显然,我漏掉了一点..
谢谢!
正如@IVan 所说,这两个表确实存在。问题是我有一个上下文生成器,它指向位于 'Persistence' 项目中的数据源。但是,WebUI 试图从其项目目录访问相同的数据源。这导致没有任何表或迁移的新数据源。
我已经通过切换到 SQLServer(本地数据库)进行开发来解决这个问题。