MCSD Entity Framework and Data Access 1 — Questions and Answers
Question 1: What is Entity Framework (EF) in the context of .NET development?
- A UI rendering framework
- An ORM that maps .NET objects to database tables (Correct answer)
- A dependency injection container
- A testing framework
Correct answer: An ORM that maps .NET objects to database tables
Entity Framework is an ORM (Object-Relational Mapper) that allows .NET developers to work with databases using objects.
Question 2: In Entity Framework Code-First, what triggers a database schema migration?
- Changing the connection string
- Running `Add-Migration` after modifying the model classes (Correct answer)
- Restarting the application
- Changing the DbContext constructor
Correct answer: Running `Add-Migration` after modifying the model classes
Running `Add-Migration` in the Package Manager Console generates a migration script based on model class changes.
Question 3: What does `DbContext` represent in Entity Framework?
- A database connection string
- The session with the database, exposing DbSet properties for querying (Correct answer)
- A model validation class
- A stored procedure wrapper
Correct answer: The session with the database, exposing DbSet properties for querying
`DbContext` is the primary class in Entity Framework that manages the database connection and entity change tracking.
Question 4: Which Entity Framework loading strategy fetches related entities in separate queries on demand?
- Eager loading
- Explicit loading
- Lazy loading (Correct answer)
- Batch loading
Correct answer: Lazy loading
Lazy loading defers loading of related entities until they are accessed, issuing additional queries at that point.
Question 5: What does the `[Key]` data annotation do in Entity Framework Code-First?
- Encrypts the property value
- Designates the property as the primary key of the entity (Correct answer)
- Makes the property required
- Sets a foreign key relationship
Correct answer: Designates the property as the primary key of the entity
The `[Key]` attribute marks a property as the primary key, used when EF cannot infer it by convention.
Question 6: What is the N+1 query problem in Entity Framework?
- Inserting N rows requires N+1 transactions
- Loading N entities with lazy loading triggers N additional queries for related data (Correct answer)
- Migrations run N+1 times on startup
- N connections open simultaneously
Correct answer: Loading N entities with lazy loading triggers N additional queries for related data
N+1 occurs when loading a collection of N items and then accessing a navigation property, triggering N additional queries.
What is Entity Framework (EF) in the context of .NET development?