Entity Framework & ADO.NET quiz and interview questions
Data access is where many .NET apps get slow or insecure. Interviews ask how Entity Framework maps classes to tables, how related data is loaded, how changes are saved — and how plain ADO.NET works underneath. Test yourself below.
Entity Framework & ADO.NET: key concepts
- ORM and DbContext
- An ORM maps tables to classes. DbContext is your session with the database: it runs queries, tracks changes and saves them with SaveChanges().
- Code First and migrations
- With Code First you change C# classes and generate a migration that updates the database schema step by step.
- Loading related data
- Eager loading (Include) loads it with the query; lazy loading loads it on first access (watch for N+1); explicit loading loads it on demand.
- Change tracking
- Entities loaded by a tracking query are watched; SaveChanges writes only what changed. Use AsNoTracking for read-only lists.
- ADO.NET
- The low-level API: SqlConnection, SqlCommand with parameters, SqlDataReader for fast forward-only reads, DataSet for disconnected data.
- Safety and speed
- Always use parameters (never string-concatenated SQL), dispose connections, and batch writes instead of saving inside loops.
All 24 Entity Framework & ADO.NET questions with answers
Try each one first, then open the answer. Making a class test? Use the test maker.
What is an ORM like Entity Framework?
- A. A database server
- B. A library that maps database tables to C# classes so you can query with LINQ
- C. A backup tool
- D. A web server
Show answer
B. A library that maps database tables to C# classes so you can query with LINQ — db.Orders.Where(o => o.Total > 100) is translated into SQL for you.
What does a DbSet<Order> represent?
- A. One order
- B. The Orders table you can query and add to
- C. A database connection
- D. A migration
Show answer
B. The Orders table you can query and add to — Each DbSet on your DbContext corresponds to a table (or view) of that entity type.
What is the Code First approach?
- A. Writing SQL first
- B. Defining C# entity classes first and generating the database from them via migrations
- C. Using stored procedures only
- D. Generating classes from an existing database
Show answer
B. Defining C# entity classes first and generating the database from them via migrations — Database First is the reverse: scaffold classes from an existing database.
What are migrations for?
- A. Moving servers
- B. Applying schema changes from your model to the database in versioned steps
- C. Copying data between apps
- D. Caching queries
Show answer
B. Applying schema changes from your model to the database in versioned steps — Add a property, run dotnet ef migrations add AddPhone, and apply it — the change is tracked in source control.
Which loading strategy loads related data in the same query using Include()?
- A. Lazy loading
- B. Eager loading
- C. Explicit loading
- D. No loading
Show answer
B. Eager loading — db.Orders.Include(o => o.Customer) fetches orders and customers together, avoiding N+1 queries.
What does SaveChanges() return?
- A. The saved entity
- B. The number of entries written to the database
- C. true or false
- D. Nothing
Show answer
B. The number of entries written to the database — It returns an int — the number of state entries written (inserted, updated or deleted rows).
What is the performance problem here?
foreach (var id in ids) { var user = db.Users.Find(id); user.Active = false; db.SaveChanges(); }- A. Find is slow
- B. SaveChanges inside the loop makes a round-trip per user — save once after the loop (or use ExecuteUpdate)
- C. Active can't be changed
- D. Nothing
Show answer
B. SaveChanges inside the loop makes a round-trip per user — save once after the loop (or use ExecuteUpdate) — Batch the work: change all entities, then call SaveChanges once. EF Core 7+ can do it in one statement with ExecuteUpdate.
What does ExecuteUpdate/ExecuteDelete (EF Core 7+) do?
- A. Loads entities then updates them
- B. Runs a set-based UPDATE/DELETE in the database without loading entities
- C. Updates the schema
- D. Deletes the database
Show answer
B. Runs a set-based UPDATE/DELETE in the database without loading entities — db.Users.Where(u => u.LastLogin < cutoff).ExecuteDelete() becomes one DELETE statement.
In ADO.NET, which object runs a SQL statement?
- A. SqlConnection
- B. SqlCommand
- C. DataTable
- D. SqlBulkCopy only
Show answer
B. SqlCommand — SqlConnection opens the connection; SqlCommand holds the SQL and parameters; a reader or Execute* method runs it.
SqlDataReader vs DataSet — which is true?
- A. Both are disconnected
- B. DataReader is fast, forward-only and connected; DataSet is an in-memory, disconnected copy
- C. DataSet is faster
- D. DataReader can go backwards
Show answer
B. DataReader is fast, forward-only and connected; DataSet is an in-memory, disconnected copy — Use a reader to stream rows quickly; DataSet/DataTable when you need to work with the data offline.
Which ADO.NET method returns a single value, like a COUNT?
- A. ExecuteReader
- B. ExecuteNonQuery
- C. ExecuteScalar
- D. Fill
Show answer
C. ExecuteScalar — ExecuteScalar returns the first column of the first row. ExecuteNonQuery returns rows affected; ExecuteReader streams rows.
What is the security problem here?
var cmd = new SqlCommand( "SELECT * FROM Users WHERE Name = '" + name + "'", conn);- A. Nothing
- B. SQL injection — user input becomes part of the SQL; use parameters
- C. Missing semicolon
- D. conn must be closed first
Show answer
B. SQL injection — user input becomes part of the SQL; use parameters — Use "... WHERE Name = @name" and cmd.Parameters.AddWithValue("@name", name) (or EF, which parameterizes).
What is connection pooling?
- A. Sharing a password
- B. Reusing open database connections instead of opening a new one each time
- C. Running queries in parallel
- D. Caching query results
Show answer
B. Reusing open database connections instead of opening a new one each time — Opening connections is expensive. Dispose (close) them quickly so they go back to the pool for reuse.
Why project to a DTO with Select() instead of loading full entities for a list page?
- A. It's required
- B. It reads only the needed columns and skips change tracking — faster and lighter
- C. DTOs are cached
- D. Entities can't be serialized
Show answer
B. It reads only the needed columns and skips change tracking — faster and lighter — db.Orders.Select(o => new OrderRow(o.Id, o.Total)) generates a narrow SQL SELECT.
What does a RowVersion / concurrency token protect against?
- A. SQL injection
- B. Two users overwriting each other's changes (optimistic concurrency)
- C. Slow queries
- D. Deleted tables
Show answer
B. Two users overwriting each other's changes (optimistic concurrency) — If the row changed since you loaded it, SaveChanges throws DbUpdateConcurrencyException instead of silently overwriting.
How should you run raw SQL safely in EF Core?
- A. String concatenation
- B. FromSql / ExecuteSql with interpolated parameters, which EF turns into SQL parameters
- C. Disable validation
- D. Use dynamic
Show answer
B. FromSql / ExecuteSql with interpolated parameters, which EF turns into SQL parameters — db.Users.FromSql($"SELECT * FROM Users WHERE Email = {email}") is parameterized automatically.
What does AsNoTracking() do?
- A. Disables the database
- B. Loads entities without change tracking — faster for read-only queries
- C. Turns off lazy loading only
- D. Skips migrations
Show answer
B. Loads entities without change tracking — faster for read-only queries — Use it for list and report pages where you won't call SaveChanges on the results.
What is the DbContext?
- A. A database server
- B. Your session with the database: it queries, tracks changes and saves them
- C. A table
- D. A connection string
Show answer
B. Your session with the database: it queries, tracks changes and saves them — In ASP.NET Core it's registered as scoped — one per request.
What is the Database First approach?
- A. Writing classes first
- B. Generating entity classes and the DbContext from an existing database (scaffolding)
- C. Using only stored procedures
- D. Creating the DB with migrations
Show answer
B. Generating entity classes and the DbContext from an existing database (scaffolding) — dotnet ef dbcontext scaffold reads the schema and creates the model for you.
What is the Fluent API used for?
- A. Writing SQL
- B. Configuring the model in OnModelCreating (keys, relationships, column types) instead of attributes
- C. Logging
- D. Migrations only
Show answer
B. Configuring the model in OnModelCreating (keys, relationships, column types) instead of attributes — modelBuilder.Entity<Order>().HasMany(o => o.Items).WithOne(i => i.Order);
What is the value of state?
var product = await db.Products.FindAsync(1); product.Price = 99; var state = db.Entry(product).State;- A. Unchanged
- B. Modified
- C. Added
- D. Detached
Show answer
B. Modified — The change tracker notices the property change, so SaveChanges will send an UPDATE.
How do you save changes to two tables so that both succeed or both fail?
- A. Two separate contexts
- B. One SaveChanges call (it's already a transaction) or an explicit BeginTransaction for several calls
- C. Call SaveChanges twice quickly
- D. Use AsNoTracking
Show answer
B. One SaveChanges call (it's already a transaction) or an explicit BeginTransaction for several calls — SaveChanges wraps its commands in a transaction; Database.BeginTransactionAsync covers multiple saves.
What is a stored procedure, and how do you call one from EF Core?
- A. A C# method; call it directly
- B. Saved SQL in the database; call it with FromSql/ExecuteSql using parameters
- C. A migration; run it with dotnet ef
- D. A view; use Include
Show answer
B. Saved SQL in the database; call it with FromSql/ExecuteSql using parameters — db.Orders.FromSql($"EXEC GetOrders {customerId}") — interpolated values become parameters.
How do you configure a many-to-many relationship in EF Core 5+?
- A. It isn't supported
- B. Give both entities a collection of the other; EF creates the join table automatically
- C. Use two DbContexts
- D. Store ids as a comma list
Show answer
B. Give both entities a collection of the other; EF creates the join table automatically — Student.Courses and Course.Students — add an explicit join entity only when you need extra columns.
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.