I started evaluating .NET 5 in doing some lessons in the learn tutorials.
The Tutorial added this code to the file Program.cs
I didn't find any hint, why there is no need for an entry point main.
Is this a short form?
I am happy for every hint.
using ContosoPets.Api;
using ContosoPets.Api.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
CreateHostBuilder(args).Build().SeedDatabase().Run();
static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
static class IHostExtensions
{
public static IHost SeedDatabase(this IHost host)
{
var scopeFactory = host.Services.GetRequiredService<IServiceScopeFactory>();
using var scope = scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ContosoPetsContext>();
if (context.Database.EnsureCreated())
SeedData.Initialize(context);
return host;
}
}
Thanks
Charly