Introduction
Entity Framework Core (EF Core) is a modern Object-Relational Mapper (ORM) for .NET developers.
It allows you to work with databases using C# classes instead of writing raw SQL queries.
With EF Core, you can perform CRUD operations (Create, Read, Update, Delete) quickly, making backend development easier and cleaner.
This guide explains how beginners can perform CRUD operations in just 10 minutes using EF Core.
Step 1: Prerequisites
Before starting, make sure you have:
Visual Studio 2022 or VS Code
.NET 6 or .NET 7 SDK
SQL Server installed (or use SQL Server Express / LocalDB)
Step 2: Create a New ASP.NET Core Web API Project
Open terminal or Visual Studio:
dotnet new webapi -n EfCoreDemo
cd EfCoreDemo
dotnet run
This creates a Web API project that can be used to perform CRUD operations.
Step 3: Install EF Core Packages
Install EF Core and SQL Server provider:
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
These packages allow EF Core to work with SQL Server and create migrations.
Step 4: Create the Data Model
Create a folder Models and add Product.cs :
namespace EfCoreDemo.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
Idwill be the primary key .NameandPriceare the product properties.
Step 5: Create the Database Context
Create Data/AppDbContext.cs :
using Microsoft.EntityFrameworkCore;
using EfCoreDemo.Models;
namespace EfCoreDemo.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options) { }
public DbSet<Product> Products { get; set; }
}
}
DbSet<Product>represents the Products table in SQL Server.
Step 6: Configure Connection String
In appsettings.json , add:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=EfCoreDemoDB;Trusted_Connection=True;"
},
"AllowedHosts": "*"
}
EF Core will use this connection string to connect to SQL Server .
Step 7: Register DbContext in Program.cs
using Microsoft.EntityFrameworkCore;
using EfCoreDemo.Data;
var builder = WebApplication.CreateBuilder(args);
// Add DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
EF Core is now ready to create and manage database tables .
Step 8: Create the Database Using Migrations
Add a migration:

Join the conversation! Your thoughts help the community grow.