If you are learning .NET, you might have heard about Entity Framework Core (EF Core). It’s a tool that lets you work with databases using C# instead of writing lots of SQL queries. This makes things like saving, reading, and updating data much easier.
In this post, we will cover:
- What is Entity Framework Core (EF Core) ?
- Why Use EF Core? – Find out the benefits of using it.
- How EF Core Works – See how it works behind the scenes.
- Full Example – Look at a simple example to understand it better.
- Code First vs Database First – Learn the two ways to create your database.
- Where EF Core is Used – See real projects where EF Core is useful.
- When Not to Use EF Core – Know when it might not be the best choice.
What is Entity Framework Core?
If you’re learning .NET, you might have heard about Entity Framework Core (EF Core). EF Core is a tool that makes it easy for your C# programs to work with a database without writing a lot of SQL. It lets you perform tasks like adding, reading, updating, or deleting data using simple C# code instead of long, complicated queries.
Think of EF Core as a bridge between your app and the database. You don’t need to worry about all the database details — EF Core handles them for you. It works with many databases, such as SQL Server, MySQL, PostgreSQL, and more.
Technically, EF Core is an ORM (Object-Relational Mapper).
What does that mean ORM?
An Object Relational Model (ORM) allows your program to work with objects in code instead of dealing directly with database tables and SQL commands.
For example:
- You create a Student class → EF Core creates a table in the database.
- You create an object of that class → EF Core inserts a row in the table.
- You read a list of objects → EF Core fetches data from the table automatically.
So, instead of thinking about tables and SQL, you just work with C# classes and objects, and EF Core takes care of all the database operations behind the scenes.
In short, EF Core makes working with databases easier, faster, and less stressful, especially if you’re new to programming.
Why Use Entity Framework Core?
Using Entity Framework Core (EF Core) makes working with databases much easier. Instead of writing long SQL queries, you can use simple C# code to add, read, update, or delete data.
EF Core is widely used in Web APIs where you need to store and retrieve data efficiently. You can also check a step-by-step CRUD example to see how EF Core works in a real project.
Here’s why it’s useful:
- Write less code – Use C# instead of long SQL queries.
- Easy to manage data – Adding, updating, or deleting data is simple.
- Works with many databases – Like SQL Server, MySQL, PostgreSQL, and more.
- Keeps your app organized – Your code and database stay in sync.
In short, EF Core saves time, reduces mistakes, and makes working with databases much simpler, especially for beginners.
How EF Core Works ?
Entity Framework Core helps your app communicate with the database without writing SQL. Think of it as a helper that handles all the database work while you use simple C# code.
We’ll create models and use DbContext to interact with the database. EF Core automatically handles the SQL commands. EF Core works very well with design patterns like Repository Pattern and Dependency Injection to keep your code clean.
Here’s the basic process:
- Create a Model – Make a C# class that represents a table in your database. For example, a Product class can represent a products table.
- Create a DbContext – This is the class that connects your app to the database. It tells EF Core which tables you want to use.
- Mapping Happens Automatically – EF Core converts your classes into tables and your objects into rows behind the scenes.
- Work with Data – You can add, read, update, or delete records just by using C# code. No SQL is needed.
// Model
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
// Add a product
var product = new Product { Name = "Laptop", Price = 50000 };
context.Products.Add(product);
context.SaveChanges();In this example, EF Core automatically saves the product in the database without writing a single SQL command.
EF Core Workflow: .NET → DbContext → Database
┌───────────────┐
│ .NET App │
│ (C# Classes) │
└───────┬───────┘
│
▼
┌───────────────┐
│ DbContext │ ← EF Core connects your app to the database
└───────┬───────┘
│
▼
┌───────────────┐
│ EF Core │ ← Generates SQL commands automatically
└───────┬───────┘
│
▼
┌───────────────┐
│ Database │ ← Stores and retrieves data (SQL Server, MySQL, etc.)
└───────────────┘
This diagram shows how Entity Framework Core works. Your .NET app works with C# classes and objects. DbContext connects your app to the database, and EF Core automatically generates SQL commands to store or retrieve data. You only work with C# code, and EF Core handles all the database operations behind the scenes.
Example: Entity Framework Core
Let’s see a simple example to understand Entity Framework Core (EF Core). We’ll create a small program that saves and reads student data from a database.
We’ll create a small program that saves and reads student data from a database. For a complete real-world project, check out this Full Project / Web API example.
Step 1: Create a Student Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}Explanation:
1. This Student class represents a table in the database.
2. Each property becomes a column in the table:
- Id → Unique identifier for each student (primary key)
- Name → Student’s name
- Age → Student’s age
3. EF Core uses this class to understand what data we want to store.
Think of it like a blueprint for the database table.
Step 2: Create the Database Context
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("Server=YOUR_SERVER_NAME;Database=StudentDb;Trusted_Connection=True;");
}Explanation:
- AppDbContext is a bridge between your C# app and the database.
- DbSet tells EF Core that we want a table for students.
- OnConfiguring sets up the database connection. Replace YOUR_SERVER_NAME with your SQL Server name.
- EF Core automatically handles creating SQL queries, so you don’t need to write them manually.
Think of DbContext as the manager that talks to the database for you.
Step 3: Add and Read Data
using (var context = new AppDbContext())
{
// Add a student
var student = new Student { Name = "Alice", Age = 20 };
context.Students.Add(student);
context.SaveChanges();
// Read all students
var students = context.Students.ToList();
foreach (var s in students)
{
Console.WriteLine($"ID: {s.Id}, Name: {s.Name}, Age: {s.Age}");
}
}Explanation:
- context.Students.Add(student) → Adds a new student to the database.
- context.SaveChanges() → Saves the changes to the database. Without this, nothing will be stored.
- context.Students.ToList() → Reads all students from the database.
- The foreach loop prints each student’s details.
EF Core does all the database work in the background, so you only focus on C# code.
Summary of the Example
- Create a model → Defines the table structure.
- Create DbContext → Connects your app to the database.
- Add and read data → Shows how to save and retrieve data easily.
Want a Full Project?
If you want to see a complete real-world EF Core example with a Web API, CRUD operations, and Swagger, check out my step-by-step guide here:
Learn How to create ASP.NET Core 8 Web API – Step-by-Step CRUD with SQL Server & EF Core
Code First vs Database First approach in Entity Framework
When working with EF Core, there are two ways to create and work with your database: Code First and Database First. Both do the same thing let your C# code talk to the database but they start in different ways.
To understand how this fits into bigger apps, you can learn more about Full Stack Development.
1. Code First
Think of Code First like drawing the blueprint first before building a house.
- You start by writing your C# classes (like Student).
- EF Core reads your classes and creates the database tables automatically.
- Any time you update your classes, EF Core can update the database using migrations.
Example:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}EF Core will automatically create a table called Students with columns Id, Name, and Age.
Why use Code First?
- Best for new projects where the database doesn’t exist yet.
- Easy to make changes just by updating your code.
- Keeps your code and database in sync, so you don’t manually edit tables.
2. Database First
Think of Database First like having an existing house and making a blueprint from it.
- You start with a database that already exists.
- EF Core can generate C# classes and DbContext based on that database.
- You can then use those classes in your code to read and write data.
Why use Database First?
- Perfect if you already have a database from another project.
- Saves time because EF Core automatically creates the classes for you.
Quick Comparison
| Approach | Best For |
|---|---|
| Code First | New projects, start with code |
| Database First | Existing database, start with database |
In short:
- Code First → You write the code first, EF Core makes the database.
- Database First → You have a database first, EF Core makes the code.
Tip for Beginners:
Think of it like this:
- Code First → You’re the architect: draw the plan (classes), then EF Core builds the house (database).
- Database First → The house already exists, and EF Core helps you make a map (classes) to work with it.
Where Entity Framework Core is Used
Entity Framework Core is very popular in real-world .NET applications because it makes working with databases much easier. You don’t have to write long SQL queries — you just work with C# objects.
It can also be combined with JWT Authentication or Caching for better performance.
Here are some common places where EF Core is used:
1. Web Applications
- Many ASP.NET Core web apps use EF Core to save and read data from a database.
- Example: An online store storing products, orders, and customers.
2. APIs (Web Services)
- EF Core is widely used in Web APIs to handle data for mobile or web apps.
- Example: A student management API that stores student records and grades.
3. Desktop Applications
- Windows desktop apps can use EF Core to manage local or cloud databases.
- Example: Inventory or accounting software storing data locally or on a server.
4. Microservices
- In microservice architecture, EF Core is used by individual services to manage their own databases.
- Example: An order service in an e-commerce system handling orders independently.
In short:
EF Core is used anywhere a .NET application needs to save, read, update, or delete data in a database. It’s flexible and works with many types of apps and databases
When Not to Use Entity Framework Core
Even though EF Core is very helpful, there are some situations where it might not be the best choice.
1. Very Simple Projects
If your project only has a few tables or very simple data operations, using EF Core can be overkill. Plain SQL or Dapper might be easier.
2. High-Performance Requirements
EF Core is slower than raw SQL in some cases. If your app needs extremely fast database operations, writing SQL manually might be better.
For tips on improving performance, check our guide on Performance Optimization.
3. Complex Queries
For very complex SQL queries, EF Core can sometimes be tricky. Writing SQL directly gives you more control.
4. Tiny or Lightweight Apps
If your app doesn’t need a full database layer or ORM, EF Core may add unnecessary complexity.
In short:
Use EF Core when you want easy, clean database handling. Avoid it when you need maximum speed, very simple setups, or very complex SQL queries.
Conclusion
Entity Framework Core (EF Core) makes working with databases in .NET much easier. Instead of writing long SQL queries, you can use simple C# code to add, read, update, or delete data.
You can also learn more about deploying your .NET apps to the cloud.
We learned:
- What EF Core is – a tool that connects your app to a database.
- Why use it – it saves time, reduces mistakes, and works with many databases.
- How it works – create models, use DbContext, and EF Core handles the database.
- Code First vs Database First – start with code or start with a database.
- Where it’s used – web apps, APIs, desktop apps, and microservices.
- When not to use it – very simple projects, high-performance needs, or complex queries.
Related Articles
- Difference between .NET Framework, .NET Core & .NET 8
- What is .NET Full Stack Development? Beginner Guide – Understand the full .NET full stack development.
- What Is ASP.NET MVC Framework? Architecture, Features, Life Cycle & Example – Learn ASP.NET MVC
- What Is Web API in .NET? Explained Simply
- Learn How to Create ASP.NET Core 8 Web API (CRUD Guide)
- How to Implement JWT Authentication in ASP.NET Core 8
- How to Implement Caching in ASP.NET Core 8 Web API (Types & Examples)
- What Is Middleware in ASP.NET Core 8? Request Pipeline Explained with Custom Middleware Example
- Azure for .NET Developers: Beginner Guide
- How to Optimize ASP.NET Core 8 Web API Performance
- Dependency Injection in .NET Core
- Repository Pattern in .NET Core Web API

