.net-core - 无法在 dotnet Core 2.1 中创建新角色
问题描述
我一直在阅读 dotnet core Authentication Documentation 并尝试做一些示例。我能够创建新用户,但在创建新角色时遇到了问题。当我调用 RoleManager.CreateAsync 时,它不会创建新角色。
应用上下文类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<string>, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
启动.cs:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration["AppSetting:DefaultConnection"]));
services.AddIdentity<ApplicationUser, IdentityRole<string>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我的存储库:
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signManager;
private readonly ApplicationDbContext _context;
private readonly RoleManager<IdentityRole<string>> _roleManager;
public AccountRespository(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signManager, ApplicationDbContext context, RoleManager<IdentityRole<string>> roleManager)
{
_signManager = signManager;
_userManager = userManager;
_context = context;
_roleManager = roleManager;
}
我在转发中创建角色方法
public async Task CreateRoles(string roleName)
{
await _roleManager.CreateAsync(new IdentityRole() { Name = "Admin" });
}
解决方案
当我调用 CreateRole 方法时,上下文已被释放。我将 create 方法添加到接口并从控制器调用它。它现在按预期工作。
推荐阅读
- python - 如何使用python从XML中单个根下的数据框创建多个子元素(有子元素)?
- python - CEWR Literals 的 Pylint 含义
- python - 将带有其他组件的不断更新的动画情节放在 tkinter gui 中
- amazon-linux - 在 Amazon Linux 上安装 fasd
- scala - 如何从控制台中删除 ASCII Spark Banner?
- php - 当未选中复选框时,我想显示数据库中的所有数据
- javascript - 具有放大缩放效果的无限旋转木马
- typescript - TypeScript 元组条件比较总是计算为 false
- reactjs - React 16 / Router V4中显示抽屉时如何覆盖后退按钮
- php - 如何修复 Yii2 中的“子目录中的静态页面”问题