C# AggregateException:发生了一个或多个错误。(无法解析作用域类型)
我试图用一个静态类为我的身份数据库种子 以下是我的种子方法:C# AggregateException:发生了一个或多个错误。(无法解析作用域类型),c#,.net,entity-framework,core,C#,.net,Entity Framework,Core,我试图用一个静态类为我的身份数据库种子 以下是我的种子方法: public static async Task CreateIdentityUsers(IServiceProvider service,IConfiguration configuration) { var userManager = service.GetRequiredService<UserManager<AlGecUser>>();
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);
}
}
}
以下是我的启动方法:
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();
}
如何修复它?不应通过启动添加种子数据,而应通过
ef migrations
命令添加种子数据。让它做它打算做的工作。如果您需要确保数据在启动时存在,请考虑在启动时运行迁移 不应通过启动添加种子数据,而应通过ef migrations
命令添加种子数据。让它做它打算做的工作。如果您需要确保数据在启动时存在,请考虑在启动时运行迁移 您没有显示启动方法,但它看起来像是在依赖项注入容器初始化之前调用此方法。我在底部调用它,在所有调用之后。好吧,您仍然需要显示启动方法。我们没什么可依据的,我把它寄了。感谢您尝试帮助我。那么ConfigureServices
呢?您没有显示启动方法,但看起来您在依赖项注入容器初始化之前调用了此方法。在调用所有内容之后,我在底部调用它。嗯,您仍然需要显示启动方法。我们没什么可依据的,我把它寄了。谢谢你帮我。那ConfigureServices
呢?
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.)