Entity Framework Core Interview Questions
DbContext lifecycle, migrations, Code First, relationships, N+1 problem, EF Core vs Dapper. Critical for any .NET data access role.
Q1: What is Entity Framework Core?
ORM (Object-Relational Mapper) โ maps C# classes to database tables.
Write C# objects instead of SQL. EF Core generates the SQL.
DbContext โ database connection + unit of work
DbSet<T> โ table representation
builder.Services.AddDbContext<SchoolDb>(o =>
o.UseSqlServer(connectionString));
Q2: What is Code First approach?
Define C# classes (entities) โ EF Core creates database schema.
Opposite: Database First โ generate C# classes from existing database.
Code First flow:
1. Create entity classes (Student, Teacher)
2. Create DbContext with DbSet properties
3. dotnet ef migrations add InitialCreate
4. dotnet ef database update
โ Database and tables created automatically
Q3: What is a Migration?
# Create migration (snapshot of model changes)
dotnet ef migrations add AddStudentEmail
# Apply to database
dotnet ef database update
# Rollback
dotnet ef database update PreviousMigrationName
# Remove last migration (if not applied)
dotnet ef migrations remove
Q4: What is the difference between eager, lazy, and explicit loading?
// Eager loading โ load related data immediately with Include
var students = await db.Students
.Include(s => s.ExamMarks) // load in same query
.Include(s => s.FeeAccount)
.ToListAsync();
// Lazy loading โ load related data on demand (requires proxies, virtual nav props)
// student.ExamMarks accessed โ triggers another SQL query automatically
// Explicit loading โ manually load when needed
var student = await db.Students.FindAsync(id);
await db.Entry(student).Collection(s => s.ExamMarks).LoadAsync();
Q5: What is the N+1 query problem?
// BAD โ N+1 problem: 1 query for students + 1 query per student for marks
var students = await db.Students.ToListAsync();
foreach (var student in students)
{
Console.WriteLine(student.ExamMarks.Count); // triggers separate query each time!
}
// 1 + N queries for N students
// GOOD โ 1 query with Join
var students = await db.Students
.Include(s => s.ExamMarks)
.ToListAsync();
// 1-2 queries total
Q6: What is DbContext lifecycle? What is Scoped lifetime?
DbContext should be Scoped (one per HTTP request).
Never Singleton โ DbContext is not thread-safe.
Never Transient โ too much overhead.
builder.Services.AddDbContext<SchoolDb>(o => ...);
// AddDbContext registers as Scoped automatically
Reason: Each request needs its own unit of work (change tracker, connection).
Q7: What is AsNoTracking?
// Default โ EF Core tracks all loaded entities (change tracking overhead)
var students = await db.Students.ToListAsync();
// AsNoTracking โ no tracking, faster for read-only queries
var students = await db.Students.AsNoTracking().ToListAsync();
// Use AsNoTracking for:
// - Read-only GET endpoints
// - Reports, exports
// - Any query where you don't need to update the entity
Q8: What is the difference between EF Core and Dapper?
EF Core:
+ Full ORM โ handles relationships, migrations, change tracking
+ Less code for CRUD operations
+ Code First โ define schema in C#
- Slower for complex queries
- Generated SQL sometimes not optimal
Use for: standard CRUD, relationships, new projects
Dapper:
+ Micro ORM โ raw SQL with C# object mapping
+ Very fast (close to raw ADO.NET)
+ Full SQL control
- No migrations, no change tracking
- More code for relationships
Use for: complex reports, performance-critical queries, existing DB
Real-world: Use BOTH in same project โ EF Core for CRUD, Dapper for complex queries.
Q9: What is Fluent API vs Data Annotations?
// Data Annotations โ attributes on entity class
public class Student
{
[Key]
public int Id { get; set; }
[Required, MaxLength(100)]
public string Name { get; set; } = "";
[Column("class_name")]
public string ClassName { get; set; } = "";
}
// Fluent API โ in OnModelCreating (more powerful, separates config from model)
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<Student>(e => {
e.HasKey(s => s.Id);
e.Property(s => s.Name).IsRequired().HasMaxLength(100);
e.Property(s => s.ClassName).HasColumnName("class_name");
});
}
Q10: What is SaveChanges and when does it send SQL?
- N+1 problem โ explain and fix with Include
- AsNoTracking โ when and why
- EF Core vs Dapper โ honest comparison
- DbContext lifetime โ always Scoped
- Code First migrations โ explain the flow
Use ChatGPT, Claude, or Copilot to go deeper on Entity Framework Core interview questions. Try these prompts:
"Ask me 10 EF Core interview questions and evaluate my answers""Explain the N+1 query problem in EF Core with a School Management example""When would you use Dapper instead of EF Core? Ask me this interview question""Quiz me on EF Core migrations โ ask a scenario-based question"
๐ก Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" โ fastest way to find edge cases and deepen understanding.