Scaffold template for a Web API with PostgreSQL on Heroku
-
While developing locally, you can change the PostgreSQL connection string in
DeltaxImdbApi\Program.cs
-
public static void Main(string[] args) { // While developing locally, set your Heroku database connection string here. // When ready to deploy to Heroku, remove this code. DeltaxImdbApiConfiguration.ConnectionString = "PUT_HEROKU_DATABSE_CONNECTION_STRING_HERE"; CreateHostBuilder(args).Build().Run(); }
-
When finished with local development, remove this. Heroku’s database credentials can change anytime - hence, it is recommended to always read the connection string from the environment variable. The
DeltaxImdbApiConfiguration
class handles this for you.- Please see this SO answer.
-
-
If you are using Migrations for the database, please read these instructions.
-
Example code in
C#
-
// This automatically gives you a connection to the heroku database using (var connection = DeltaxImdbApiConfiguration.GetConnection()) { connection.Open(); // Do some meaningful work with connection connection.Close(); }
-