ASP.NET Core is a framework that is structured and type-safe, which means it helps us write code in an organized way and catches mistakes before they cause problems. Today we are going to look at how to connect our application to a database using PostgreSQL. To do this, we first need to create an entity or model, which defines the shape of the data we want to work with. Then we need a database context, which acts as a bridge between our application and the database, allowing us to read and write data easily.
Now let’s create a model. In ASP.NET Core, a model defines the structure of the data we want to work with. Here’s an example of a simple User model:
namespace backend_01.Core.Model
{
public class User
{
public int Id { get; set; }
public string? UserName { get; set; } = string.Empty;
public string? Email { get; set; } = string.Empty;
public string? Password { get; set; } = string.Empty;
public DateTime createdAt { get; set; } = DateTime.Now;
}
}
Let’s break this down:
Id is an integer that uniquely identifies each user.
UserName, Email, and Password are all strings. The ? means they can be null, and = string.Empty ensures they start as empty strings so our app doesn’t crash if nothing is provided.
createdAt is a DateTime property that stores when the user was created. By default, it is set to the current time using DateTime.Now.
Now that we have our model ready, we need something that connects our application to the database — this is where the DbContext comes in. You can think of the DbContext as the main bridge between your C# code and the actual database. It helps us send and receive data in an organized way. Here’s the code:
using backend_01.Core.Model;
using Microsoft.EntityFrameworkCore;
namespace backend_01.Infrastructure.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
}
}
Let’s understand it in simple terms:
ApplicationDbContext is a class that inherits from DbContext. This makes it capable of communicating with the database.
The line public ApplicationDbContext(DbContextOptions options) : base(options) passes configuration options (like the database connection string) to the base DbContext class.
public DbSet Users { get; set; } tells Entity Framework that we want a table in the database for our User model. Here, DbSet represents that table, and each User object will become one record (or row) inside it.
So, whenever we perform operations like adding, updating, deleting, or fetching users, we’ll do it through this ApplicationDbContext. Think of it as your app’s direct hotline to the database.
You should always use the database context after creating the builder and before building the app instance. This is because we need to register all our services (like controllers, DbContext, authentication, etc.) before the application starts running.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
)
In this guide, we learned how ASP.NET Core helps us build structured and type-safe applications that stay clean and organized. We created a simple User model to define our data structure, then built an ApplicationDbContext class to connect our application with the database. Finally, we registered our DbContext in the Program.cs file, ensuring that the app can communicate with PostgreSQL smoothly. With these steps, our application is now ready to perform database operations like saving, updating, or retrieving data — all in a clean and professional way.
Top comments (1)
Great tutorial for getting started! The explanation of DbContext is super clear for newcomers.
A logical next step for anyone following this would be to implement the Repository Pattern on top of this DbContext. It's a great way to decouple the database logic from the business logic, making the application much easier to test and maintain in the long run.
Thanks for putting this together!