Featured Article

Master Entity Framework Tutorial .NET Core Step by Step

Kenneth Jul 13, 2026

.NET Core has significantly streamlined the development process, and the Entity Framework Core (EF Core) is a vital tool for managing database interactions within this framework. This tutorial will guide you through integrating EF Core into your .NET Core project, ensuring a seamless database context setup for your applications.

Free Entity Framework Book
Free Entity Framework Book

EF Core is an object-database mapper that makes it possible to work with a database by programming .NET objects. It simplifies data querying by providing an easy-to-use interface for relational databases. Let's start by understanding how to set up your database context with EF Core in .NET Core.

Entity Framework Core Database-First Tutorial for .NET Core
Entity Framework Core Database-First Tutorial for .NET Core

Setting up the Database Context

To begin, you'll need to install the Microsoft.EntityFrameworkCore package and the corresponding SQL provider, for instance, Microsoft.EntityFrameworkCore.SqlServer for SQL Server databases. You can do this through the Package Manager Console in Visual Studio:

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

``` Install-Package Microsoft.EntityFrameworkCore.SqlServer ```

Next, create a derived context class from DbContext that maps to your database:

```csharp public class ApplicationDbContext : DbContext { public DbSet Blogs { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Blogging;Trusted_Connection=True;"); } } ```

Database Schema Migration

Entity Framework Core In 60 Minutes : Entity Framework Core Tutorial
Entity Framework Core In 60 Minutes : Entity Framework Core Tutorial

EF Core's DbContext lets you create, update, and delete database schema using migrations. Here's how to migrate the database:

  1. In your Package Manager Console, run the following commands to create and apply migrations:
  2. Add-Migration InitialCreate
    Update-Database

Adding and Removing Data

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

Now that you have a functioning database context, you can add data methods to your DbContext:

```csharp public class ApplicationDbContext : DbContext { // ... public void AddBlog(Blog blog) { Blogs.Add(blog); SaveChangesAsync(); } public void RemoveBlog(Blog blog) { Blogs.Remove(blog); SaveChangesAsync(); } } ```

Or you can use the DbSet's AddAsync and RemoveAsync methods directly:

```csharp public void AddBlog(Blog blog) { Blogs.AddAsync(blog); } public void RemoveBlog(Blog blog) { Blogs.RemoveAsync(blog); } ```

With these methods, you can easily manage database records directly from your .NET Core application.

Entity Framework Core Code-First Tutorial
Entity Framework Core Code-First Tutorial

Querying Data using LINQ

EF Core supports querying data using LINQ. Here's an example of retrieving all blogs and their posts:

Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)
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
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)
Step by Step Tutorial - .Net Core MVC REST API
Step by Step Tutorial - .Net Core MVC REST API
ASP.NET Core Web API with Code First Entity Framework for Beginners Part-1
ASP.NET Core Web API with Code First Entity Framework for Beginners Part-1
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
Real World Tutorials, Happy Coding!: Hour 11 ASP.NET Core : Entity Framework Core Models For Northwind Cafe
Real World Tutorials, Happy Coding!: Hour 11 ASP.NET Core : Entity Framework Core Models For Northwind Cafe
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

```csharp var blogs = await context.Blogs.Include(b => b.Posts).ToListAsync(); ```

Filtering Data

You can fetch data based on specific conditions using LINQ's method syntax.

```csharp var csharpBlogs = await context.Blogs.Include(b => b.Posts) .Where(b => b.Name == "C# Blog").ToListAsync(); ```

Ordering and Paging Data

LINQ allows you to sort and page your results.

```csharp var posts = await context.Posts.Include(p => p.Blog) .OrderByDescending(p => p.Blog.PublishedOn) .Skip(10) .Take(10) .ToListAsync(); ```

Now, as you've seen, integrating EF Core into your .NET Core project can significantly enhance your development experience. Continued exploration of EF Core's features will enable you to leverage its full power in your applications!