Skip to content

Instantly share code, notes, and snippets.

@Ibro
Created May 24, 2021 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/e7f629399e9cbf43df7f99c6273fc30c to your computer and use it in GitHub Desktop.
Save Ibro/e7f629399e9cbf43df7f99c6273fc30c to your computer and use it in GitHub Desktop.
public static IHost SeedIfNeeded(this IHost host) {
using var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<IDbInitializer>>();
try {
logger.LogInformation("Checking if Database is created and if seed needs to be done.");
var dbInitializer = services.GetRequiredService<IDbInitializer>();
dbInitializer.Seed().GetAwaiter().GetResult();
}
catch(Exception ex) {
logger.LogError(ex, "An error occurred while seeding the database.");
}
return host;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment