[英]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.