从.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)
.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();
});
}
}
IWebHostExtensions.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace Microsoft.AspNetCore.Hosting
{
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())
{
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;
}
}
}
信用:
答案 0 :(得分:1)
您在构建器上多次致电.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
{
//...
再次在Main
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(); //<-- 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;
}
}
并在构建虚拟主机后调用它。
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();
}