【发布时间】:2020-03-02 07:48:36
【问题描述】:
我正在开发一个项目,我正在使用代码优先方法,我正在使用 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");
});
显然,我错过了一点..
谢谢!
【问题讨论】:
-
AspNetRoles和AspNetUserRoles是不同的表,两者都必须存在。检查迁移是否应用于连接字符串指向的数据库。 -
我已经应用了迁移。我还删除了数据库并再次应用迁移,然后更新数据库。
标签: asp.net-core entity-framework-core asp.net-identity