Featured Article

.Net Core Entity Framework Example Tutorial With Real World CRUD App

Kenneth Jul 13, 2026

In the dynamic world of web development, Microsoft's .NET Core has emerged as a powerful cross-platform framework that streamlines the process of developing modern web applications and APIs. When it comes to interacting with databases, Entity Framework (EF) is an unparalleled ORM (Object-Relational Mapping) tool that seamlessly connects .NET Core applications to a whole variety of databases. This article guides you through a practical example of integrating Entity Framework with a .NET Core application.

Free Entity Framework Book
Free Entity Framework Book

Before diving into the example, let's briefly understand why Entity Framework is an excellent choice for .NET Core applications. EF simplifies data access by exposing objects that map to database tables, columns, and relationships. It generates type-safe query methods and database context for working with data, making your code more robust and less prone to errors. Now, let's get our hands dirty with an example.

#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini
#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini

Setting Up Entity Framework in a .NET Core Project

To begin, you need to install the necessary packages for Entity Framework Core in your .NET Core project. In a console application, for instance, you'll add the following packages to your project.json or csproj file:

the architecture diagram for an application
the architecture diagram for an application
  1. Microsoft.EntityFrameworkCore - The core package for Entity Framework Core.
  2. Microsoft.EntityFrameworkCore.SqlServer (or another database provider) - The database-specific provider package.
  3. Microsoft.EntityFrameworkCore.Tools - Provides useful tools like EF migrations and EF database update.

In a .NET Core web application, you would also add these packages using the Microsoft.AspNetCore.App metapackage. Once the packages are installed, you can proceed to create your database context and models.

Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?
Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?

Defining the Database Context

The database context is a central part of an EF application, representing the database and exposing methods for querying, updating, and saving data. Let's create a simple context for a Blog database.

Note: The following code is a concise example, and real-world contexts may be more complex. ```csharp using Microsoft.EntityFrameworkCore; public class BloggingContext : DbContext { public DbSet Blogs { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Blogging;"); } } ```

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

Creating Models for Your Data

In this example, the Blog model represents a blog in the database. Models are plain old CLR objects (POCOs) that map to database tables and columns. We'll also create a simple BlogPost model to illustrate a one-to-many relationship.

  1. ```csharp public class Blog { public int Id { get; set; } public string Url { get; set; } public List Posts { get; set; } } ```
  2. BlogPost.cs ```csharp public class BlogPost { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } } ```
Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)

The `Posts` property in the Blog class and the `Blog` property in the BlogPost class establish a one-to-many relationship between these models.

Migrating to the Database

the asp net core info sheet shows what it is like to work on an application
the asp net core info sheet shows what it is like to work on an application
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
Code First Approach in Entity Framework in Asp.net MVC with Example - Tutlane
A Complete Guide to Microsoft .NET Framework
A Complete Guide to Microsoft .NET Framework
.NET Core vs .NET Framework: What CTOs Must Know in 2026
.NET Core vs .NET Framework: What CTOs Must Know in 2026
Change Tracking in ASP.NET Core | Entity Framework | C#
Change Tracking in ASP.NET Core | Entity Framework | C#
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
a poster with the words, using sol server & ef core
a poster with the words, using sol server & ef core
the internet architecture diagram for all major architectures in net core, including two different types of
the internet architecture diagram for all major architectures in net core, including two different types of
a family tree with all the names and numbers
a family tree with all the names and numbers

EF Core's migrations feature enables you to create and update your database schema directly from your code. To create an initial migration based on our models, run the following command in the Package Manager Console:

Add-Migration InitialCreate

This command generates a migration file that represents the current state of your models. You can apply this migration to your database using the following command:

Update-Database

Querying and Adding Data

Now that we have a database and some models, let's see how to query and add data using Entity Framework Core.

  1. Querying Blogs and Their Posts ```csharp var blogs = await _context.Blogs .Include(b => b.Posts) .ToListAsync(); ```
  2. Adding a Blog and Its Initial Post ```csharp var blog = new Blog { Url = "http://www.contoso.com" }; _context.Blogs.Add(blog); blog.Posts = new List { new BlogPost { Title = "Hello", Content = "This is my first post." } }; await _context.SaveChangesAsync(); ```

By including blog posts when querying blogs, we're able to load related data in a single database query, improving the application's performance. Adding a blog and its first post demonstrates how easy it is to work with related data using Entity Framework Core.

Managing Database Changes with Migrations

Throughout the development process, you may need to alter your models to match changes in your database schema. EF Core's migrations feature is perfect for maintaining a database up-to-date with your models. To create a new migration, run the following command:

Add-Migration NewBlogPostField

This command analyzes the changes in your models since the last migration and creates a new migration file ;class. Once you've reviewed and adjusted the generated migration code, apply it to your database using the Update-Database command. This ensures your database remains synchronized with your codebase.

Entity Framework Core offers a powerful and flexible way to interact with databases in .NET Core applications. Its extensive query facilities, support for complex relationships, and convenient migrations feature make it an invaluable tool for any .NET developer. As you continue to explore EF Core, you'll find new ways to streamline your data access and improve your applications' performance.

Now that you've seen Entity Framework Core in action, take the next step in your learning journey by trying out more advanced features like interpolated strings, global filters, and database view support. Happy coding!