Вы звоните .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();
}