PrepPilotPractise free

Entity Framework Core interview questions

DbContext, tracking, migrations, loading, performance

17 questions · DbContext · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is the purpose of the DbContext in Entity Framework Core?

DbContext manages the database connection, tracks changes to entities, and coordinates saving data to the database. It's the main class for interacting with the data model in EF Core.

When should you use AsNoTracking() in a query?

I use AsNoTracking when I only need to read data and don't plan to modify or save the entities. This improves performance by skipping change tracking.

How do you apply a migration to your database in EF Core?

After creating a migration with 'dotnet ef migrations add', I apply it using 'dotnet ef database update', which updates the database schema to match the model.

What is eager loading and how do you use it?

Eager loading fetches related data as part of the initial query using the Include method. For example, context.Orders.Include(o => o.Items) loads orders and their items together.

Intermediate questions

Explain the unit of work pattern in the context of DbContext.

DbContext implements the unit of work pattern by tracking all changes to entities during its lifetime and saving them in a single transaction when SaveChanges is called. This ensures consistency and atomicity for a set of operations.

What is the N+1 query problem and how can you avoid it in EF Core?

The N+1 problem happens when EF Core issues one query for the main entities and then one query per related entity, leading to many database calls. I avoid it by using eager loading with Include or by restructuring queries to fetch all needed data in fewer calls.

How do you execute a raw SQL query in EF Core?

I use FromSqlRaw or FromSqlInterpolated methods on a DbSet. For example, context.Users.FromSqlRaw("SELECT * FROM Users WHERE IsActive = 1").ToList() executes the given SQL and maps the results to entities.

What is a concurrency token and how does EF Core use it?

A concurrency token is a property, like a RowVersion, that EF Core checks during updates to detect if another process has changed the data. If the token has changed, EF Core throws a concurrency exception, allowing me to handle conflicts.

Describe the difference between lazy loading and explicit loading.

Lazy loading automatically loads related entities when they're accessed, while explicit loading requires me to call Load() on a navigation property. Both can be useful, but lazy loading can cause hidden performance issues if not managed carefully.

How can you improve the performance of queries that are executed frequently with different parameters?

I use compiled queries in EF Core, which cache the query plan and reduce overhead for repeated execution. This is especially helpful for high-traffic endpoints with similar queries.

What tools or techniques do you use to diagnose EF Core performance issues?

I enable EF Core logging to inspect generated SQL, use ToQueryString() to review queries, and sometimes use database profilers or Application Insights to monitor query performance and identify bottlenecks.

What happens if you call SaveChanges multiple times within the same DbContext instance?

Each call to SaveChanges persists the current tracked changes to the database in a transaction. However, if entities remain tracked, further changes can be saved in subsequent calls, but this can lead to unexpected behavior if not managed carefully.

Senior questions

How would you handle a DbUpdateConcurrencyException in a production application?

See the answer in the app, free

Describe a scenario where you would use raw SQL instead of LINQ in EF Core.

See the answer in the app, free

What are the risks of using a long-lived DbContext instance?

See the answer in the app, free

How would you optimize EF Core for bulk inserts or updates?

See the answer in the app, free

How do you manage EF Core migrations in a team environment with multiple developers?

See the answer in the app, free

More .NET topics