我在迁移配置 class 中的播种器锁定了应用程序(Asp.net MVC)

My seeder in the Migration Configuration class locks the application (Asp.net MVC)

我有以下迁移配置 class:

namespace MVC_Authentication.Migrations
{
    using Microsoft.AspNet.Identity.EntityFramework;
    using MVC_Authentication.Models;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Threading.Tasks;

internal sealed class MigrationConfiguration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public MigrationConfiguration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        ContextKey = "MVC_Authentication.Models.ApplicationDbContext";
    }

    protected override void Seed(ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data.
        SeedDatabase(context).GetAwaiter().GetResult();

    }

    protected async Task SeedDatabase(ApplicationDbContext ctx)
    {
        var roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(ctx));

        if (await roleManager.FindByNameAsync("Administrator") == null)
            await roleManager.CreateAsync(new IdentityRole("Administrator"));

        if (await roleManager.FindByNameAsync("User") == null)
            await roleManager.CreateAsync(new IdentityRole("User"));

        ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(ctx));

        var user_Admin = new ApplicationUser()
        {
            SecurityStamp = Guid.NewGuid().ToString(),
            UserName = "Admin",
            Email = "myEmail",
            EmailConfirmed = true
        };
        if (await userManager.FindByNameAsync(user_Admin.UserName) == null)
        {
            await userManager.CreateAsync(user_Admin, "MyPassword");
            await userManager.AddToRoleAsync(user_Admin.Id, "User");
            await userManager.AddToRoleAsync(user_Admin.Id, "Administrator");
        }
    }
}

}

但是当执行到达时:

if (await roleManager.FindByNameAsync("Administrator") == null)

应用程序锁定,我可以等啊等。 也许我不应该在这里使用 RoleManager 和 UserManager?这是播种角色和用户的唯一方法。 感谢您提供任何可能出错的提示。

尝试在 RoleManager 中使用同步方法,而不是在 ApplicationRoleManager 中;

var roleStore = new RoleStore<IdentityRole>(db);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.FindName(...);