Featured Article

Build a Fast Asp Net Core Web Api Tutorial With Entity Framework Step By Step

Kenneth Jul 13, 2026

Embarking on a journey to create powerful web APIs using ASP.NET Core and managing your data with Entity Framework (EF)? You've come to the right place! In this comprehensive tutorial, we'll guide you through the process of building an API from scratch, incorporating database operations seamlessly with EF Core.

Folder Structure of ASP.NET MVC Application
Folder Structure of ASP.NET MVC Application

ASP.NET Core is a cross-platform framework for developing modern, cloud-based, Internet-connected applications. Paired with EF Core, an Object-Relational Mapping (ORM) library, they form a robust duo for building APIs with efficient data management. Let's dive right in!

Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)

Setting Up Your ASP.NET Core Web API Project

First, ensure you have the .NET SDK installed. Then, create a new ASP.NET Core Web API project using the following command in your terminal:

a web api with asp net core, net 6 0 build a web api with asp net core
a web api with asp net core, net 6 0 build a web api with asp net core

dotnet new webapi -n MyApiProject

Adding Entity Framework Core to the Project

Detailed ASP.NET MVC Pipeline
Detailed ASP.NET MVC Pipeline

Add EF Core to your project by right-clicking on your solution in Visual Studio and selecting "Manage NuGet Packages". Search for Microsoft.EntityFrameworkCore and install it.

Next, add a reference to the SQL Server provider if you're using SQL Server. Search for Microsoft.EntityFrameworkCore.SqlServer and install it. For other database providers, search and install the appropriate package.

Defining the Data Model

Asp.net Core web API Tutorial: C# web API .Net Core Example
Asp.net Core web API Tutorial: C# web API .Net Core Example

Create a new folder named Models and add a C# class representing your data entity. For instance, create a Blog.cs file with the following content:

public class Blog { public int Id { get; set; } public string Name { get; set; } }

Now, let's configure EF Core to use this model in our application.

How to call Stored Procedures in ASP.NET Core
How to call Stored Procedures in ASP.NET Core

Configuring Entity Framework Core

Create a new class named ApplicationDbContext.cs and add DbContext functionality to it. Your class should look like this:

GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)
What is the difference between ASP.NET and ASP.NET Core?
What is the difference between ASP.NET and ASP.NET Core?
ASP.NET and MVC Tutorial Guide
ASP.NET and MVC Tutorial Guide
Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
API Gateway 101
API Gateway 101
ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core
frontend
frontend

public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Blog> Blogs { get; set; } }

Here, we've created a DbSet for the Blog entity. This DbSet is used to interact with the Blog table in our database.

Configuring the Database Connection

In the appsettings.json file, add your database connection string:

"ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyApiProject;Trusted_Connection=True;MultipleActiveResultSets=true" }

Next, update the ConfigureServices method in Startup.cs to configure EF Core with your connection string:

services.AddDbContext<ApplicationDbContext>( options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

Migrating the Database

Create a new migration with the following command:

dotnet ef migrations add InitialCreate -p .\Models

Apply this migration to your database:

dotnet ef database update

Now, your database schema is updated to match your data model.

Creating API Controllers

Right-click on your project in Solution Explorer and select "Add" > "Controller" > "API Controller with read/write actions". Name it BlogsController.cs.

Implementing CRUD Operations

In the newly created controller, you'll find scaffolded methods for Create, Read, Update, and Delete (CRUD) operations. Implement these methods using the DbContext to interact with the database.

Start your API and test it using tools like Postman or curl. You should now have a functioning API with database operations managed by EF Core.

Continuously expanding your API's functionality and maintaining high-quality code will set you up for success. Happy coding, and here's to your API-forward journey!