In this article, we are going to learn how to deploy a .NET API to Heroku through GitHub Actions. Also, we are going to create a PostgreSQL database in Heroku.
Prerequisites
- Visual Studio 2022 with .NET 6 SDK
- pgAdmin and PostgreSQL Database
- GitHub account
- Heroku account
1. Create .NET project
Create a .NET Web API project, named NETToDoDemo. Select .NET 6 framework, authentication None, disable Configure for HTTPS, enable Docker, and select Windows as Docker OS.


Remove WeatherForecast class and WeatherForecastController controller.

Right click to the project / Edit Project file. Then, disable nullable property.

Create Entities folder and inside this create ToDo class.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace NETToDoDemo.Entities
{
public class ToDo
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(30)]
public string Name { get; set; }
public string? Description { get; set; }
[Range(1, 3)]
public int Priority { get; set; }
public bool IsCompleted { get; set; }
}
}
Install the following packages:
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.Tools
- Npgsql.EntityFrameworkCore.PostgreSQL
Create Contexts folder, and inside this create ToDoContext class.
using Microsoft.EntityFrameworkCore;
using NETToDoDemo.Entities;
namespace NETToDoDemo.Contexts
{
public class ToDoContext : DbContext
{
public ToDoContext(DbContextOptions<ToDoContext> options) : base(options)
{
}
public DbSet<ToDo> ToDos { get; set; }
}
}
Open pgAdmin and create ToDoDemo database.

Open appsettings.Development.json file and add the connection string.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=ToDoDemo;Username=postgres;Password=12345678"
}
}
In the Program.cs file we are going to get the connection string and configure the Db Context.
using Microsoft.EntityFrameworkCore;
using NETToDoDemo.Contexts;
var builder = WebApplication.CreateBuilder(args);
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
builder.Configuration.AddConfiguration(configurationBuilder.Build());
// Add services to the container.
var defaultConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ToDoContext>(options =>
options.UseNpgsql(defaultConnectionString));
...
Open Package Manager Console. And execute the following commands:
- Add-Migration InitialCreate -Context StoreContext
- Update-Database

Go to pgAdmin, ToDoDemo database, and expand Schemas / public / Tables. As you can see there are 2 tables.

In the .NET project, right click to Controllers / Add / Controller ..., then select API, API Controller with actions, using Entity Framework.

Use the below configuration and click on Add.

Make sure it's selected IIS Express as profile. Then, go to Debug tab / Start Without Debugging.

All the methods from ToDosController are available to test.

Let's test all the methods. First, create a ToDo.

As you can see, the ToDo was created in the database.

Then, we are going to update it. Go to the PUT method. Don't forget to specify the id in the parameters.

Test GET ToDos and GET ToDos by id.


Finally, test the DELETE method.

And if you test GET /api/ToDos again, the list should be empty.
























Uday DodiyaPosted Aug 28, 2022, 4:15 AM
Thank You So Much For This
Abir MahmudPosted Jun 29, 2022, 12:16 PM
Thank you very much. This article was the only resource which I was able to execute successfully.