创建管理员用户后启动控制器的问题

创建管理员用户后,当前控制器出现问题。这是用于创建管理员用户的代码:


  protected override void Initialize(RequestContext requestContext)

    {

        using (var ac = new ApplicationDbContext())

        {

            var userManager = new ApplicationUserManager(new 

            UserStore<ApplicationUser>(ac));

            var roleManager = new RoleManager<IdentityRole>(new 

            RoleStore<IdentityRole>(ac));

            var user = new ApplicationUser { UserName = "hello@gmail.com", 

            Email = "hello@gmail.com" };

            userManager.Create(user, "Hello1234!");

            roleManager.Create(new IdentityRole("admin"));


            user = userManager.FindByNameAsync(user.UserName).Result;


            userManager.AddToRole(user.Id, "admin");

        }

    }

由于错误而更新


当我导航到产品控制器时,在浏览器中出现此错误:


Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 


Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


Source Error: 


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.





慕村9548890
浏览 104回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP