2017-08-28 61 views
3
//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(); 

這段代碼生成一個Admin角色帳戶並最終將該帳戶添加到管理員角色。 過去,這工作之前,我已經升級到2.0.0在Startup.cs中生成角色在.NET Core 2.0中不起作用

現在我得到這個錯誤:

System.InvalidOperationException occurred HResult=0x80131509
Message=Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' from root provider.

這怎麼能在此版本中做了什麼?謝謝!

+1

這可能對您有所幫助。請檢查https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0 – immirza

+0

謝謝!我使用Program.cs的.net 1.1方式,明確的方式,現在一切正常! – FabiGhenof

回答

2

SOLUTION

新的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

相關問題