ASP.NET Core Entity Framework Core 2 — Questions and Answers
Question 1: What does AsNoTracking() do in an EF Core LINQ query?
- Returns entities without adding them to the change tracker, improving read performance (Correct answer)
- Disables lazy loading for the query
- Prevents the query from being cached
- Marks entities as read-only in the schema
Correct answer: Returns entities without adding them to the change tracker, improving read performance
AsNoTracking() skips the overhead of the change tracker for read-only scenarios, reducing memory and CPU usage.
Question 2: Which EF Core method applies pending migrations to the database?
- dotnet ef database update (Correct answer)
- dotnet ef migrations apply
- dotnet ef db migrate
- dotnet ef schema update
Correct answer: dotnet ef database update
dotnet ef database update applies all pending migrations, creating or altering tables as defined in the migration files.
Question 3: What is the purpose of the Fluent API in Entity Framework Core?
- To configure entity mappings and relationships in OnModelCreating using method chaining (Correct answer)
- To write raw SQL with a fluent syntax
- To define navigation properties on entities
- To configure DI for EF Core services
Correct answer: To configure entity mappings and relationships in OnModelCreating using method chaining
The Fluent API in OnModelCreating() allows detailed configuration of columns, keys, indexes, and relationships beyond data annotations.
Question 4: Which EF Core loading strategy fetches related entities in the same query using a JOIN?
- Eager loading with Include() (Correct answer)
- Lazy loading
- Explicit loading with Load()
- Deferred loading
Correct answer: Eager loading with Include()
Eager loading via Include() fetches related entities in a single SQL query using JOINs, avoiding the N+1 query problem.
Question 5: What does the [Key] attribute do on an entity property in EF Core?
- Designates the property as the primary key for the entity (Correct answer)
- Creates a database index on the column
- Marks the property as required
- Encrypts the column value
Correct answer: Designates the property as the primary key for the entity
[Key] overrides EF Core's convention of using 'Id' or '{TypeName}Id' as the primary key, explicitly designating the property.
Question 6: Which EF Core query returns only the first matching entity or null if not found?
- FirstOrDefaultAsync() (Correct answer)
- SingleOrDefaultAsync()
- FindAsync()
- FirstAsync()
Correct answer: FirstOrDefaultAsync()
FirstOrDefaultAsync() returns the first matching entity or null without throwing, while FirstAsync() throws if no match exists.
What does AsNoTracking() do in an EF Core LINQ query?