Asp.net core 使用实体框架核心迁移创建角色
我正在使用EF Core(2.0.0)创建第四个迁移脚本。在这里,我想向数据库添加一些角色 问题是,我真的不知道怎么做。目前我有:Asp.net core 使用实体框架核心迁移创建角色,asp.net-core,entity-framework-core,entity-framework-migrations,Asp.net Core,Entity Framework Core,Entity Framework Migrations,我正在使用EF Core(2.0.0)创建第四个迁移脚本。在这里,我想向数据库添加一些角色 问题是,我真的不知道怎么做。目前我有: protected override void Up(MigrationBuilder migrationBuilder) { // todo: Pass connection string somehow..? var opt = new DbContextOptions<ApplicationContext>(); var r
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...
//}
}
protected override void Up(MigrationBuilder MigrationBuilder)
{
//todo:以某种方式传递连接字符串。。?
var opt=new DbContextOptions();
var roleManager=新角色管理器(新角色存储(新应用上下文(opt));
//如果(!roleManager.RoleExists(“角色名称”))
//{
//todo:创建角色。。。
//}
}
但是这样创建rolemager
会产生以下错误:
没有给出与所需的形式化参数相对应的参数
的参数“roleValidators”
“RoleManager.RoleManager(IRoleStore,
IEnumerable>,ILookupNormalizer,
IdentityErrorDescriber,ILogger>)'
我不知道如何解决这个问题。我找不到有关如何使用迁移在.NET Core中正确执行此操作的任何信息
我在这段代码中面临两个问题:
- 我试图以某种方式创建DbContext的实例。我不应该从迁移代码中获取DbContext吗
- 像这样实例化
不起作用,需要解决角色管理器
如何解决这些问题?方法基本上是一个指令文件,告诉EF的database migrator如何生成数据库升级脚本。该方法在生成脚本时执行。在那里做任何数据操作都是完全不合适的。EF core还不支持种子设定,因此您必须在应用程序启动时添加缺少的角色,例如通过以下方式