Sunday, November 16, 2025

EF - Entity Framework Core

 ✅ 1. What is Entity Framework Core? (Interview-ready definition)

EF Core is Microsoft’s Object-Relational Mapper (ORM) that lets you interact with a database using .NET classes instead of SQL queries.
It handles:

  • Database Connection & Commands

  • Mapping classes ↔ tables

  • CRUD operations

  • Migrations

  • Change Tracking

  • LINQ queries

Interview one-liner:

EF Core is an ORM that allows developers to work with databases using C# objects, eliminating most SQL and improving productivity while maintaining performance.


2. EF Core Workflow (Very Important for Interviews)

Model (C# classes) ↓ DbContext (Mapping + Configuration) ↓ Database (Migrations) ↓ LINQ Queries (CRUD)

3. Hands-On Practical Example (Minimal, Perfect for Learning)

Let’s create a simple Employee Management app with EF Core.


🟦 Step 1: Install EF Core Packages

Run in terminal:

dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools

🟦 Step 2: Create Model Class

public class Employee { public int Id { get; set; } public string Name { get; set; } public string Department { get; set; } public decimal Salary { get; set; } }

🟦 Step 3: Create DbContext

using Microsoft.EntityFrameworkCore; public class AppDbContext : DbContext { public DbSet<Employee> Employees { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) { options.UseSqlServer("Server=.;Database=EFCoreDemo;Trusted_Connection=True;"); } }

🟦 Step 4: Create Database Using Migrations

dotnet ef migrations add InitialCreate dotnet ef database update

This creates your database + Employees table.

In interviews, they often ask:
What are migrations?
Answer:
Migrations help you evolve your database schema over time while keeping data safe.


🟦 Step 5: Perform CRUD Operations


Insert Data

using var context = new AppDbContext(); var emp = new Employee { Name = "John", Department = "IT", Salary = 50000 }; context.Employees.Add(emp); context.SaveChanges();

Read Data (LINQ)

var employees = context.Employees.ToList();

Update

var emp = context.Employees.First(); emp.Salary = 60000; context.SaveChanges();

Delete

var emp = context.Employees.First(); context.Employees.Remove(emp); context.SaveChanges();

4. Frequently Asked EF Core Interview Questions (with answers)

1. What is DbContext?

DbContext is the main class that manages database connections, CRUD operations, and mapping between classes and tables.


2. What is DbSet?

DbSet represents a table. It lets you query and save instances of a model.


3. What is Change Tracking?

EF Core automatically tracks object changes and updates only modified fields.


4. What are Migrations?

Migrations manage schema changes without dropping the database.


5. What is Lazy Loading, Eager Loading, Explicit Loading?

  • Eager Loading: Includes related data immediately using .Include()

  • Lazy Loading: Related data is loaded automatically when accessed

  • Explicit Loading: Manually load related data using Entry(entity).Collection().Load()


6. How to use LINQ with EF Core?

Example:

var itEmployees = context.Employees .Where(e => e.Department == "IT") .ToList();

5. Relationship Example (Highly asked in interviews)

Example: One-to-Many

Employee → Projects
One employee can have many projects.


Models:

public class Employee { public int Id { get; set; } public string Name { get; set; } public List<Project> Projects { get; set; } } public class Project { public int Id { get; set; } public string Title { get; set; } public int EmployeeId { get; set; } public Employee Employee { get; set; } }

DbContext Mapping:

public DbSet<Employee> Employees { get; set; } public DbSet<Project> Projects { get; set; }

Query with include:

var employees = context.Employees .Include(e => e.Projects) .ToList();

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...