Featured Article

.net Entity Framework Example Tutorial Guide

Kenneth Jul 13, 2026

The Entity Framework (EF) is a modern object-database mapper (ORM) that enables .NET developers to work with relational databases using .NET objects. By using EF, developers can interact with databases in a more efficient and intuitive manner, reducing boilerplate code and enhancing application performance. Let's dive into an example to illustrate how the Entity Framework operates in a .NET context.

Free Entity Framework Book
Free Entity Framework Book

Before we proceed, ensure you have a basic understanding of the following concepts: databases (specifically, SQL Server for this example), .NET platform, and object-oriented programming. Having knowledge of LINQ (Language Integrated Query) is also beneficial, but not mandatory, as EF supports LINQ for querying data.

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

The AdventureWorks Sample Database

The AdventureWorks database, a widely-used sample database from Microsoft, provides a great starting point for our EF example. It mimics a fictional bicycle manufacturer's database, containing multiple tables such as Products, Categories, and Customers.

Database First Approach of Entity Framework in Asp.Net MVC 4 Example - Tutlane
Database First Approach of Entity Framework in Asp.Net MVC 4 Example - Tutlane

For this tutorial, we'll create a simple console application using .NET Core and Entity Framework Core (EF Core). EF Core is a lightweight, extensible version of EF that provides the same robust feature set for modern web apps and services.

Setting Up the Project

Asp.Net MVC Entity Framework Database First Approach Example - Tutlane
Asp.Net MVC Entity Framework Database First Approach Example - Tutlane

First, create a new .NET Core Console Application project. Then, add the Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer NuGet packages to your project.

Create a model class representing the 'Products' table in AdventureWorks using Entity Framework's data annotations. Here's a basic example:

```csharp using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; public class Product { public int ProductId { get; set; } [Required] [StringLength(50)] public string Name { get; set; } [Column(TypeName = "money")] public decimal ListPrice { get; set; } } ```

Creating the DbContext Class

A Complete Guide to Microsoft .NET Framework
A Complete Guide to Microsoft .NET Framework

Next, create a DbContext class that will define the database context for your application. This class will inherit from DbContext and configure the models using DbSet properties.

Here's an example of a DbContext class with a DbSet of Products:

```csharp using Microsoft.EntityFrameworkCore; public class AdventureWorksContext : DbContext { public DbSet Products { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(local);Database=AdventureWorks;Trusted_Connection=True;"); } } ```

Querying and Manipulating Data Using EF Core

GraphQL with Entity Framework Core || Basic Example || ASP.NET Core
GraphQL with Entity Framework Core || Basic Example || ASP.NET Core

Now that we have our DbContext class set up, we can use LINQ queries to retrieve and manipulate data from the AdventureWorks database.

Add the following code to your Main method to fetch and display all products:

#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
Introduction to Entity Framework Core - The Engineering Projects
Introduction to Entity Framework Core - The Engineering Projects
.NET Core vs .NET Framework: What CTOs Must Know in 2026
.NET Core vs .NET Framework: What CTOs Must Know in 2026
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
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
CRUD Operations using ADO.Net Entity Framework in Asp.Net MVC Example - Tutlane
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
the architecture diagram for an appliance and application layer, with text below it
the architecture diagram for an appliance and application layer, with text below it
Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)
a diagram showing the different types of web services and what they are used to create them
a diagram showing the different types of web services and what they are used to create them

```csharp using (var context = new AdventureWorksContext()) { var products = context.Products.ToList(); foreach (var product in products) { Console.WriteLine($"{product.ProductId}: {product.Name} - ${product.ListPrice:N2}"); } } ```

Adding, Updating, and Deleting Entities

EF Core enables database operations such as inserting, updating, and deleting entities with minimal code. Here are examples of each operation using the Product class:

Inserting:

```csharp var newProduct = new Product { Name = "EF Example Product", ListPrice = 19.99m }; context.Products.Add(newProduct); context.SaveChanges(); ```

Updating:

```csharp var productToUpdate = context.Products.Find(1); productToUpdate.ListPrice = 24.99m; context.SaveChanges(); ```

Deleting:

```csharp var productToDelete = context.Products.Find(1); context.Products.Remove(productToDelete); context.SaveChanges(); ```

Remember to wrap your database operations within the appropriate 'using' blocks to ensure that the connection is disposed properly after use.

Migrations and Database Initialization

Entity Framework Core uses migrations to make changes to your database schema. Before running migrations, ensure that your database (AdventureWorks, in this case) exists in your SQL Server instance.

Run the following commands to create and apply migrations, followed by updating the database:

```shell dotnet ef migrations add InitialCreate dotnet ef database update ```

The first command creates a new migration with the given name (InitialCreate), while the second command applies the pending migrations to your database.

As you've seen, Entity Framework Core simplifies and streamlines database operations in .NET applications. Keep exploring its features and capabilities to further enhance your development workflow.

Now that you have a solid understanding of how EF Core works with .NET, why not give it a try in your next project? Happy coding!