Featured Article

Master ASP NET Core Entity Framework Tutorial Step by Step

Kenneth Jul 13, 2026

Embarking on a journey to master ASP.NET Core and Entity Framework? You're in the right place! This comprehensive tutorial will lead you step by step through the integration of these powerful tools, helping you build robust, data-driven web applications. Let's dive in!

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

ASP.NET Core, Microsoft's fast, cross-platform, high-performance framework, and Entity Framework Core, its lightweight, extensible, and cross-platform persistence platform, are a match made in heaven. Together, they empower developers to create stunning web apps with efficient data handling and manipulation. So, let's set up our first project and get started!

Create an ASP .NET Core Site with Entity Framework - Kill All Defects
Create an ASP .NET Core Site with Entity Framework - Kill All Defects

Setting Up the Project and Environment

The first step is to install the necessary tools and create your ASP.NET Core project with Entity Framework Core. Open your terminal or command prompt, navigate to your desired project location, and run:

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

dotnet new webapp -n MyApp --framework netcoreapp3.1

This command creates a new ASP.NET Core Web Application using the latest .NET Core version. Now, it's time to add Entity Framework Core to our project.

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

Adding Entity Framework Core to the Project

To add Entity Framework Core, run the following command in your terminal:

dotnet add package Microsoft.EntityFrameworkCore

How to Use JQuery DataTables with ASP.NET Core Web API
How to Use JQuery DataTables with ASP.NET Core Web API

This command adds the Entity Framework Core package to your project. Note that you'll also need to add the package for the database provider you're using. For instance, for a SQL Server database, run:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Creating the Model and DbContext

Tutorial: EF Core: Building an ASP.NET MVC Application using the Code-First Approach
Tutorial: EF Core: Building an ASP.NET MVC Application using the Code-First Approach

Now that Entity Framework Core is installed, let's create a simple model and DbContext class to interact with our database. In the Models folder, create a new class, Product.cs:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload
ASP.NET Core + Entity Framework Core + jQuery to Delete Records without Page Reload
Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)
A Guide for Building Angular SPA with ASP.NET Core 5 Web API
A Guide for Building Angular SPA with ASP.NET Core 5 Web API
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
Web Application, 10 Things
Web Application, 10 Things
An introduction to OpenID Connect in ASP.NET Core
An introduction to OpenID Connect in ASP.NET Core
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Compilemode    Online Tutorials IoT, Microsoft Azure, AWS, ASP.NET Core, C#,Amazon Web Service,SQL,CosmosDB, React, Angular
Compilemode Online Tutorials IoT, Microsoft Azure, AWS, ASP.NET Core, C#,Amazon Web Service,SQL,CosmosDB, React, Angular

Next, create a ApplicationDbContext.cs class in the root of your project folder:

using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

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

Migrations and Initializing the Database

With our model and DbContext set up, it's time to create and apply migrations to our database. Run the following commands:

dotnet ef migrations add InitialCreate

This command adds a new migration named InitialCreate based on our Product model. Now, apply this migration to your database:

dotnet ef database update

Upon successful execution, your database is initialized with a table named Products, ready to store your application's product data.

Incorporating the DbContext into the Project

To use the DbContext class in our ASP.NET Core application, we need to register it in the dependency injection system. Open the Startup.cs file and modify the ConfigureServices method as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>();
    services.AddControllersWithViews();
}

Now, you can inject ApplicationDbContext into your controllers using dependency injection:

public class HomeController : Controller
{
    private readonly ApplicationDbContext _context;

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

    // ...
}

With ASP.NET Core and Entity Framework Core successfully integrated, you're well on your way to building powerful web applications. Happy coding, and remember to explore and adapt these techniques to fit your specific project needs!