繁体   English   中英

在Startup.cs中生成角色在.NET Core 2.0中不再起作用

[英]Generating Roles in Startup.cs doesn't work anymore in .NET Core 2.0

//Creates the admin ROLE
new RoleSeed(app.ApplicationServices.GetService<RoleManager<IdentityRole>>()).Seed().GetAwaiter().GetResult();
//Creates the admin ACCOUNT
new AccountSeed(app.ApplicationServices.GetService<UserManager<ApplicationUser>>()).Seed().GetAwaiter().GetResult();
//Adding ACCOUNT to ROLE
new AccToRoleSeed(app.ApplicationServices.GetService<UserManager<ApplicationUser>>(),
    app.ApplicationServices.GetService<ApplicationDbContext>()).Seed().GetAwaiter().GetResult();

这段代码生成管理员角色,帐户,最后将该帐户添加到管理员角色。 这曾经在我升级到2.0.0之前工作

现在我收到这个错误:

发生System.InvalidOperationException HResult = 0x80131509
Message = 无法从根提供程序解析作用域服务'Microsoft.AspNetCore.Identity.RoleManager`1 [Microsoft.AspNetCore.Identity.IdentityRole]'。

如何在这个版本中完成? 谢谢!

新的2.0 Program.cs默认一个衬垫不包括这样做,但是,显式方式仍然有效:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
          .UseKestrel()
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseIISIntegration()
          .UseStartup<Startup>()
          .Build();

        host.Run();
    }
}

此外,在创建之前不检查角色是否存在不再被原谅:D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM