简体   繁体   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();

This piece of code generates an Admin role, account and finally adds that account to the admin role. 这段代码生成管理员角色,帐户,最后将该帐户添加到管理员角色。 This used to work before I upgraded to 2.0.0 这曾经在我升级到2.0.0之前工作

Now I get this error: 现在我收到这个错误:

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

How can this be done in this version? 如何在这个版本中完成? Thanks! 谢谢!

SOLUTION

The new 2.0 Program.cs default one liner doesn't include doing this, however, the explicit way still works: 新的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();
    }
}

Also, not checking if the Role exists before creating is not forgiven anymore :D 此外,在创建之前不检查角色是否存在不再被原谅:D

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

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