C# 错误WebHostBuilder仅允许创建WebHost的单个实例
从.NET Core 2.0升级到.NET Core 2.1 错误 WebHostBuilder只允许创建WebHost的单个实例C# 错误WebHostBuilder仅允许创建WebHost的单个实例,c#,.net,entity-framework,asp.net-core,C#,.net,Entity Framework,Asp.net Core,从.NET Core 2.0升级到.NET Core 2.1 错误 WebHostBuilder只允许创建WebHost的单个实例 Program.cs namespace WebApp1 { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args) .Migrate
Program.cs
namespace WebApp1
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.MigrateDbContext<ApplicationDbContext>((context, services) =>
{
var env = services.GetService<IHostingEnvironment>();
var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();
new ApplicationDbContextSeed()
.SeedAsync(context, env, logger)
.Wait();
}).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddEnvironmentVariables();
});
}
}
名称空间WebApp1
{
公共课程
{
公共静态void Main(字符串[]args)
{
CreateWebHostBuilder(args)
.MigrateDbContext
您正在生成器上多次调用.Build()
扩展中的第一个
public static class IWebHostExtensions
{
public static IWebHostBuilder MigrateDbContext<TContext>(this IWebHostBuilder webHost, Action<TContext, IServiceProvider> seeder) where TContext : DbContext
{
using (var scope = webHost.Build().Services.CreateScope()) //<-- HERE
{
//...
链接的示例使用主机而不是构建器来执行迁移
重构扩展,类似于链接示例的扩展
public static class IWebHostExtensions {
public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext, IServiceProvider> seeder)
where TContext : DbContext {
using (var scope = webHost.Services.CreateScope()) {
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>();
try {
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
context.Database
.Migrate();
seeder(context, services);
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
} catch (Exception ex) {
logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
}
}
return webHost;
}
}
公共静态类IWebHostExtensions{
公共静态IWebHost MigrateDbContext(此IWebHost webHost,操作播种器)
其中TContext:DbContext{
使用(var scope=webHost.Services.CreateScope()){
var services=scope.ServiceProvider;
var logger=services.GetRequiredService();
var context=services.GetService();
试一试{
logger.LogInformation($“正在迁移与上下文{typeof(TContext.Name})关联的数据库”;
上下文数据库
.Migrate();
播种机(上下文、服务);
logger.LogInformation($“与上下文{typeof(TContext.Name})关联的迁移数据库”);
}捕获(例外情况除外){
logger.LogError(例如,$”迁移上下文{typeof(TContext.Name})上使用的数据库时出错;
}
}
返回webHost;
}
}
并在构建web主机后调用它
public class Program {
public static void Main(string[] args) {
BuildWebHost(args)
.MigrateDbContext<ApplicationDbContext>((context, services) => {
var env = services.GetService<IHostingEnvironment>();
var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();
new ApplicationDbContextSeed()
.SeedAsync(context, env, logger)
.Wait();
})
.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) => {
config.AddEnvironmentVariables();
})
.Build();
}
公共类程序{
公共静态void Main(字符串[]args){
BuildWebHost(args)
.MigrateDbContext((上下文、服务)=>{
var env=services.GetService();
var logger=services.GetService();
新应用程序dbContextSeed()
.SeedAsync(上下文、环境、记录器)
.Wait();
})
.Run();
}
公共静态IWebHost BuildWebHost(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.ConfigureAppConfiguration((builderContext,config)=>{
config.AddEnvironmentVariables();
})
.Build();
}
您正在两次构建WebHost实例,一次是在扩展方法中,另一次是在Main()中。这可能是错误告诉您的。
public static class IWebHostExtensions {
public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext, IServiceProvider> seeder)
where TContext : DbContext {
using (var scope = webHost.Services.CreateScope()) {
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<TContext>>();
var context = services.GetService<TContext>();
try {
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
context.Database
.Migrate();
seeder(context, services);
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
} catch (Exception ex) {
logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
}
}
return webHost;
}
}
public class Program {
public static void Main(string[] args) {
BuildWebHost(args)
.MigrateDbContext<ApplicationDbContext>((context, services) => {
var env = services.GetService<IHostingEnvironment>();
var logger = services.GetService<ILogger<ApplicationDbContextSeed>>();
new ApplicationDbContextSeed()
.SeedAsync(context, env, logger)
.Wait();
})
.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.ConfigureAppConfiguration((builderContext, config) => {
config.AddEnvironmentVariables();
})
.Build();
}