I am trying to migrate an IHost extension for EF Seed migration from .Net5 to .Net6 . Can you please lead me the proper way to do this conversion.
public static class HostExtensions
{
public static IHost MigrateDatabase<TContext>(this IHost host,
Action<TContext, IServiceProvider> seeder,
int? retry = 0) where TContext : DbContext
{
int retryForAvailability = retry.Value;
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
<code removed for brevity>
}
return host;
}
}
The extension is being called in .Net5 as follows:
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.MigrateDatabase<OrderContext>((context, services) =>
{
var logger = services.GetService<ILogger<OrderContextSeed>>();
OrderContextSeed
.SeedAsync(context, logger)
.Wait();
})
.Run();
}
CodePudding user response:
MigrateDatabase :
public static IHost MigrateDatabase(this IHost host)
{
using var scope = host.Services.CreateScope();
var sp = scope.ServiceProvider;
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
// operations
return host;
}
Program.cs :
var builder = WebApplication.CreateBuilder(args);
builder.Host
.Build()
.MigrateDatabase();
var app = builder.Build();
apo.Run();