Get Started with DigitalOcean using ASP.NET: Chapter 4 Installing Entity Framework

We will now install Entity Framework Core for PostgreSQL. The reason we are choosing PostgreSQL is because we can create a managed database in DigitalOcean platform. We will also install Entity Framework Core Design for creating database migrations. We will use the following commands to install it:

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools

Make sure you install the packages compatible with the .NET Core version your application is. For this project, we are installing version 6.0.x.

Once the packages are installed, we will add our Database Context class that inherits from DbContext.


We will add another class for our Notes. This is just for example purposes so you can use your own model class.


Now we will create a managed database in DigitalOcean.


You can choose the region that is closer to you. Make sure to choose PostgreSQL.


The size and resources of the database for this book are the minimum but you can configure based on your requirements.


Once the database is created, you can find the connection string and credentials on the same page.


We will register the database service just before “var app = builder.Build();”.

var connectionString =
    \";\" +
    \"Port=25060;\" +
    \"Database=defaultdb;\" +
    \"Uid=doadmin;\" +
    \"Pwd=AVNS_h2LAREw0KACGaaN0SzH;\" +
    \"SslMode=Require;\" +
    \"Trust Server Certificate=true\";

    (options => options.UseNpgsql(connectionString));

The above server and password will be according to your project provided by DigitalOcean.

Once all is set, we will run our first migration in Package Manager Console as follows:

Add-Migration Initial.

We will see the following class after Add-Migration is successfully executed.


Afterwards, we will run to apply these changes in our database.


We will add the following two methods for adding and reading the notes:

app.MapPost(\"/add-note\", async (Note note, DigitalOceanDbContext db) =>
    await db.AddAsync(note);
    await db.SaveChangesAsync();

app.MapGet(\"/get-notes\", async (DigitalOceanDbContext db) =>
    var notes = await db.Notes.ToListAsync();
    return notes;

Once these are added, we will commit our code and push it to GitHub and then delete the existing deploy.

Now that the APIs are created and successfully deployed, we will test the APIs using Postman. Postman is a tool for testing APIs and is available for free to use.

We will make a POST call to add a note.


We will make a GET call to get all the notes saved.



For more details, you can always visit
The source code can be found on

All of the API keys shared in this project will not be useful as thos





Leave a Reply

Your email address will not be published. Required fields are marked *