This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//program.cs | |
var app = builder.Build(); | |
var scope = app.Services.CreateScope(); | |
await DataHelper.ManageDataAsync(scope.ServiceProvider); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |