What is Entity Framework Core complete guide with example 2026

What is Entity Framework Core? Complete Guide With Example.

What is Entity Framework Core?

What does that mean ORM?

Why Use Entity Framework Core?

How EF Core Works ?

// 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();

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.)
└───────────────┘

Example: Entity Framework Core

Step 1: Create a Student Model

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

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;");
}

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}");
    }
}

Summary of the Example

Want a Full Project?

Code First vs Database First approach in Entity Framework

1. Code First

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Why use Code First?

2. Database First

Why use Database First?

Quick Comparison

Where Entity Framework Core is Used

1. Web Applications

2. APIs (Web Services)

3. Desktop Applications

4. Microservices

When Not to Use Entity Framework Core

1. Very Simple Projects

2. High-Performance Requirements

3. Complex Queries

4. Tiny or Lightweight Apps

Conclusion

🚀 Subscribe for Tech Updates

📧

👤

We respect your privacy. Unsubscribe anytime.

Leave a Comment

Your email address will not be published. Required fields are marked *