AggregateException:发生一个或多个错误。 (无法解析作用域类型)

时间:2019-05-09 18:55:10

标签: c# .net entity-framework core

我正在尝试为我的Identity数据库添加静态类。

这是我的播种方法:

public static async Task CreateIdentityUsers(IServiceProvider service,IConfiguration configuration)
        {


            var userManager = service.GetRequiredService<UserManager<AlGecUser>>();
            var roleManager = service.GetRequiredService<RoleManager<AlGecRole>>();

            var username = configuration["Data:AdminUser:username"];
            var email = configuration["Data:AdminUser:email"];
            var password = configuration["Data:AdminUser:password"];
            var role = configuration["Data:AdminUser:role"];

            if (await userManager.FindByNameAsync(username) == null)
            {
                if (await roleManager.FindByNameAsync(role) == null)
                {
                    await roleManager.CreateAsync(new AlGecRole(role));
                }

                AlGecUser user = new AlGecUser()
                {
                    UserName = username,
                    Email = email,
                    Name = "SampleName",
                    Surname = "SampleSurname"
                };

                IdentityResult result = await userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, role);
                }

            }

        }

在启动时,我这样调用此方法:

 SeedIdentity.CreateIdentityUsers(app.ApplicationServices, Configuration).Wait();

这是我的启动方法:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<AlGecDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MainRepositoryCS")));
        services.AddDbContext<AlGecIdentityDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("IdentityRepositoryCS")));

        services.AddIdentity<AlGecUser,AlGecRole>()
            .AddEntityFrameworkStores<AlGecIdentityDbContext>()
            .AddDefaultTokenProviders();

        services.AddTransient<IProductRepository, EfProductRepository>();
        services.AddTransient<IUnitOfWork, EfUnitOfWork>();
        services.AddTransient<IAdvertisementRepository, EfAdvertisementRepository>();
        services.AddTransient<ICategoryRepository, EfCategoryRepository>();
        services.AddTransient<ISubCategoryRepository, EfSubCategoryRepository>();
        services.AddTransient<ICartService, CartService>();
        services.AddTransient<ICartSessionService, CartSessionService>();
        services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();

        services.AddSession();
        services.AddMvc();
    }
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseSession();
    app.UseAuthentication();
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
    });

    SeedIdentity.CreateIdentityUsers(app.ApplicationServices, Configuration).Wait();
}

但是当我尝试打开我的网站时,它抛出了一个错误

AggregateException: One or more errors occurred. (Cannot resolve scoped service 'Microsoft.AspNetCore.Identity.UserManager`1[AlGec.ECommerce.WebUI.IdentityCore.AlGecUser]' from root provider.)

我该如何解决?

1 个答案:

答案 0 :(得分:0)

不应通过启动来添加种子数据,而应使用ef migrations命令。让它完成预期的工作。如果需要确保在启动时存在数据,请考虑在启动时运行迁移。