Featured Article

Add Entity Framework to ASP NET Core - A Step-by-Step Guide

Kenneth Jul 13, 2026

Integrating Entity Framework (EF) into an ASP.NET Core application is a common practice, enabling developers to interact with databases using a high-level, object-oriented approach. If you're new to EF or have been wondering how to add it to your ASP.NET Core project, this guide will walk you through a comprehensive, step-by-step process.

the architecture diagram for an application
the architecture diagram for an application

Before we dive into the configuration process, ensure you have the following prerequisites: a database ready for use, and an ASP.NET Core web application up and running. This guide assumes you're using SQL Server, but the process is similar for other databases compatible with EF Core.

Tutorial: Create a more complex data model for an ASP.NET MVC app
Tutorial: Create a more complex data model for an ASP.NET MVC app

Setting up Entity Framework Core

First, let's install the required NuGet packages. In the Package Manager Console, run the following commands to install EF Core and the SQL Server provider:

ASP.NET Tutorial | ASP.NET Core Tutorial For Begginers
ASP.NET Tutorial | ASP.NET Core Tutorial For Begginers

Install-Package Microsoft.EntityFrameworkCore

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects

Creating the DbContext class

The DbContext class is the heart of EF, responsible for configuring the database and managing connectivity. Create a new class named 'ApplicationDbContext' in your project:

using Microsoft.EntityFrameworkCore;

Folder Structure in Asp.Net MVC Project (Application) - Tutlane
Folder Structure in Asp.Net MVC Project (Application) - Tutlane

public class ApplicationDbContext : DbContext
{
    public DbSet<YourEntity> YourEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=YourDatabase;Trusted_Connection=True;");
    }
}

Configuring the startup class

Next, modify the Startup.cs file to include middleware for EF. Add the DbContext to the dependency injection (DI) container within the ConfigureServices method:

services.AddDbContext<ApplicationDbContext>();

Creating the database and migrations

Free Entity Framework Book
Free Entity Framework Book

Now that EF is set up, let's create the database and perform initial migrations. Open the Package Manager Console and run the following commands:

Add-Migration InitialCreate

Web Application, 10 Things
Web Application, 10 Things
ASP.NET Framework
ASP.NET Framework
asp net core add entity framework
asp net core add entity framework
Udemy Free Courses | Learn ASP.NET MVC & Entity Framework For Free |
Udemy Free Courses | Learn ASP.NET MVC & Entity Framework For Free |
An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core
Using Multiple Databases in ASP.NET Core via Entity Framework Core
Using Multiple Databases in ASP.NET Core via Entity Framework Core
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
the diagram shows how to use apis for security and data storage, as well as other
the diagram shows how to use apis for security and data storage, as well as other
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments

This command generates an initial migration based on the DbContext configuration. It doesn't create any tables yet. To actually create them, run:

Update-Database

Managing subsequent migrations

To create new migrations, first modify your entity classes and DbContext to reflect the required changes. Then, run the following command in the Package Manager Console:

Add-Migration NewMigration

Finally, apply the new migration by running:

Update-Database

Using the database within your application

Finally, you can now use EF to interact with your database within your ASP.NET Core application. Inject the ApplicationDbContext into your controllers or services and use its methods to perform CRUD operations on your entities:

public class YourController : Controller
{
    private readonly ApplicationDbContext _context;

    public YourController(ApplicationDbContext context)
    {
        _context = context;
    }

    // Your controller actions here using _context methods
}

That's it! You've successfully integrated Entity Framework Core into your ASP.NET Core application. The next step is to start utilizing its power to efficiently manage your application's data. Happy coding!