【发布时间】:2017-09-18 20:35:16
【问题描述】:
我正在使用 EF Core (2.0.0) 创建我的第四个迁移脚本。在那里我想向数据库添加一些角色。
问题是,我不确定如何执行此操作。目前我有这个:
protected override void Up(MigrationBuilder migrationBuilder)
{
// todo: Pass connection string somehow..?
var opt = new DbContextOptions<ApplicationContext>();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationContext(opt)));
//if (!roleManager.RoleExists("ROLE NAME"))
//{
// todo: create the role...
//}
}
但是像这样创建RoleManager 会给我以下错误:
没有给出与所需形式相对应的参数 参数'roleValidators' 'RoleManager.RoleManager(IRoleStore, IEnumerable>,ILookupNormalizer, IdentityErrorDescriber, ILogger>)'
我不知道如何解决这个问题。我找不到任何有关如何在 .NET Core 中使用迁移正确执行此操作的信息。
我在这段代码中面临两个问题:
- 我正在尝试以某种方式创建 DbContext 的实例。我不应该能够从我的迁移代码中获取 DbContext 吗?
- 像这样实例化
RoleManager不起作用,需要解决。
我该如何解决这些问题?
【问题讨论】:
标签: asp.net-core entity-framework-core entity-framework-migrations