使用 entity framework 个核心迁移创建角色
Create role using entity framework core migrations
我正在使用 EF Core (2.0.0) 创建我的第 4 个迁移脚本。在那里我想向数据库添加一些角色。
问题是,我不太确定该怎么做。目前我有这个:
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
会出现以下错误:
There is no argument given that corresponds to the required formal
parameter 'roleValidators' of
'RoleManager.RoleManager(IRoleStore,
IEnumerable>, ILookupNormalizer,
IdentityErrorDescriber, ILogger>)'
我不确定如何解决这个问题。我找不到有关如何使用迁移在 .NET Core 中正确执行此操作的任何信息。
我在这段代码中遇到了两个问题:
- 我正在尝试以某种方式创建 DbContext 的实例。我不应该能够从我的迁移代码中获取 DbContext 吗?
- 像这样实例化
RoleManager
不起作用,需要解决。
如何解决这些问题?
Up
方法基本上是一个指令文件,告诉 EF 的数据库迁移器如何生成数据库升级脚本。该方法在生成脚本时执行。在那里进行任何数据操作是绝对不合适的。 EF 核心尚不支持种子设定,因此您必须在应用程序启动时添加缺少的角色,例如通过 this.
之类的内容
我正在使用 EF Core (2.0.0) 创建我的第 4 个迁移脚本。在那里我想向数据库添加一些角色。
问题是,我不太确定该怎么做。目前我有这个:
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
会出现以下错误:
There is no argument given that corresponds to the required formal parameter 'roleValidators' of 'RoleManager.RoleManager(IRoleStore, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, ILogger>)'
我不确定如何解决这个问题。我找不到有关如何使用迁移在 .NET Core 中正确执行此操作的任何信息。
我在这段代码中遇到了两个问题:
- 我正在尝试以某种方式创建 DbContext 的实例。我不应该能够从我的迁移代码中获取 DbContext 吗?
- 像这样实例化
RoleManager
不起作用,需要解决。
如何解决这些问题?
Up
方法基本上是一个指令文件,告诉 EF 的数据库迁移器如何生成数据库升级脚本。该方法在生成脚本时执行。在那里进行任何数据操作是绝对不合适的。 EF 核心尚不支持种子设定,因此您必须在应用程序启动时添加缺少的角色,例如通过 this.