這是 .NET 5 我在 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbInitializer dbInitializer) 中使用 IDbInitializer dbInitializer 現在在 .net 6 中我無法完成這項作業。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbInitializer dbInitializer)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthorization();
dbInitializer.Initialize();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
這是.NET 6.0
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
我不能像在.NET 5.0 中使用之前那樣使用 dbInitializer.Initialize() 我想在 .NET 6.0 中使用它
我怎樣才能做到這一點?請幫我。
uj5u.com熱心網友回復:
根據IDbInitializer注冊方式,您應該能夠直接從以下位置解決它app.Services:
var dbInitializer = app.Services.GetRequiredService<IDbInitializer>();
// use dbInitializer
dbInitializer.Initialize();
或者通過創建范圍ServiceProviderServiceExtensions.CreateScope:
using(var scope = app.Services.CreateScope())
{
var dbInitializer = scope.ServiceProvider.GetRequiredService<IDbInitializer>();
// use dbInitializer
dbInitializer.Initialize();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452587.html
標籤:C# 。网 asp.net 核心 .net-6.0 asp.net-core-6.0
