Skip to content

Instantly share code, notes, and snippets.

//program.cs
var app = builder.Build();
var scope = app.Services.CreateScope();
await DataHelper.ManageDataAsync(scope.ServiceProvider);
public static class ConnectionHelper
{
public static string GetConnectionString(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnection");
var databaseUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
return string.IsNullOrEmpty(databaseUrl) ? connectionString : BuildConnectionString(databaseUrl);
}
//build the connection string from the environment. i.e. Heroku
public static class DataHelper
{
public static async Task ManageDataAsync(IServiceProvider svcProvider)
{
//Service: An instance of db context
var dbContextSvc = svcProvider.GetRequiredService<ApplicationDbContext>();
//Migration: This is the programmatic equivalent to Update-Database
await dbContextSvc.Database.MigrateAsync();