×

Multiple-Choice Questions

Web Technologies MCQs

Computer Science Subjects MCQs

Databases MCQs

Programming MCQs

Testing Software MCQs

Digital Marketing Subjects MCQs

Cloud Computing Softwares MCQs

AI/ML Subjects MCQs

Engineering Subjects MCQs

Office Related Programs MCQs

Management MCQs

More

Entity Framework MCQs (Multiple-Choice Questions)

Practice Entity Framework MCQs with answers and explanations to test your knowledge of Entity Framework, Entity Framework Core, ORM concepts, DbContext, DbSet, LINQ, CRUD operations, relationships, change tracking, migrations, querying, and database configuration.

Entity Framework MCQs

These Entity Framework multiple-choice questions cover both traditional Entity Framework 6 concepts and modern Entity Framework Core concepts used in .NET applications.

List of Entity Framework MCQs

The following Entity Framework MCQs include questions on ORM, Code First, Database First, DbContext, DbSet, LINQ, tracking, relationships, migrations, loading related data, transactions, and practical EF Core code.

1. What is the primary purpose of Entity Framework?

  1. To create user interfaces for .NET applications
  2. To provide object-relational mapping between .NET objects and database data
  3. To compile C# source code into machine code
  4. To manage Windows services

Answer: B) To provide object-relational mapping between .NET objects and database data

Explanation:

Entity Framework is an object-relational mapper (ORM) that allows .NET applications to work with database data using .NET objects rather than writing most database-access code manually.

2. Which of the following is the modern, cross-platform version of Entity Framework?

  1. Entity Framework 4
  2. Entity Framework 6
  3. Entity Framework Core
  4. Entity Framework Designer

Answer: C) Entity Framework Core

Explanation:

Entity Framework Core is the lightweight, extensible, open-source, and cross-platform version of Entity Framework.

3. Which class represents a session with the database in Entity Framework Core?

  1. DbSet
  2. DbContext
  3. Database
  4. EntityContext

Answer: B) DbContext

Explanation:

DbContext represents a session with the database and provides functionality for querying, tracking, and saving entities.

4. Which namespace contains the main Entity Framework Core classes such as DbContext and DbSet?

  1. System.Data.Entity
  2. Microsoft.EntityFrameworkCore
  3. System.Data.SqlClient
  4. Microsoft.Data.Linq

Answer: B) Microsoft.EntityFrameworkCore

Explanation:

Entity Framework Core's primary APIs, including DbContext, DbSet, and model-building types, are provided by the Microsoft.EntityFrameworkCore namespace.

5. What is the purpose of a DbSet<TEntity> property in a DbContext?

  1. It represents a collection of entities of a particular type in the context
  2. It stores the connection string permanently
  3. It creates a C# namespace
  4. It represents only stored procedures

Answer: A) It represents a collection of entities of a particular type in the context

Explanation:

A DbSet<TEntity> provides access to entities of a specific type through a DbContext. It can be used for querying and saving those entities.

6. Which method is commonly overridden to configure the EF Core model using the Fluent API?

  1. ConfigureModel()
  2. OnModelCreating()
  3. BuildModel()
  4. CreateModel()

Answer: B) OnModelCreating()

Explanation:

EF Core allows model configuration through the OnModelCreating(ModelBuilder) method of a derived DbContext.

7. Which approach starts with .NET classes and can use EF to create or evolve the database schema?

  1. Code First
  2. Database First only
  3. Table First
  4. SQL First

Answer: A) Code First

Explanation:

In the Code First approach, the application's model is defined in code. EF can use that model to create or evolve the database schema through migrations.

8. What is the main idea behind the Database First approach?

  1. The database is generated only from C# classes
  2. The model is generated or designed based on an existing database
  3. No database is required
  4. All SQL queries are replaced by stored procedures

Answer: B) The model is generated or designed based on an existing database

Explanation:

Database First starts from an existing database and creates a model that represents its tables, relationships, and other supported database structures.

9. Which technology is used by Entity Framework Core to express strongly typed database queries?

  1. LINQ
  2. XML
  3. CSS
  4. Reflection-only SQL

Answer: A) LINQ

Explanation:

EF Core uses LINQ to allow developers to write strongly typed queries against entity classes. The database provider translates the query into database-specific language such as SQL.

10. Which method retrieves all matching entities from an EF Core query and materializes them into a list?

  1. ToArraySql()
  2. ToList()
  3. Materialize()
  4. FetchAll()

Answer: B) ToList()

Explanation:

ToList() executes the query and materializes the results into a List<T>. The asynchronous equivalent is ToListAsync().

11. Which LINQ method is commonly used to filter entities?

  1. Where()
  2. FilterBy()
  3. FindWhere()
  4. Restrict()

Answer: A) Where()

Explanation:

The LINQ Where() method applies a predicate to filter the query results.

12. Which method orders query results in ascending order based on a specified key?

  1. OrderAscending()
  2. OrderBy()
  3. SortAsc()
  4. Arrange()

Answer: B) OrderBy()

Explanation:

OrderBy() orders the query by the specified key in ascending order. OrderByDescending() is used for descending order.

13. Which method is used to retrieve exactly one matching element and throws if multiple elements are found?

  1. First()
  2. Single()
  3. FirstOrDefault()
  4. FindAny()

Answer: B) Single()

Explanation:

Single() expects exactly one matching element. It throws an exception if no element or more than one element satisfies the condition.

14. What is the main difference between FirstOrDefault() and SingleOrDefault()?

  1. FirstOrDefault() allows multiple matches and returns the first, while SingleOrDefault() expects at most one
  2. Both always return every matching entity
  3. SingleOrDefault() always returns the first entity
  4. FirstOrDefault() can only be used with SQL Server

Answer: A) FirstOrDefault() allows multiple matches and returns the first, while SingleOrDefault() expects at most one

Explanation:

FirstOrDefault() returns the first matching element or the default value when none exists. SingleOrDefault() returns the only matching element or the default value, but throws if more than one match exists.

15. Which method saves pending changes in an EF Core context?

  1. CommitChanges()
  2. SaveChanges()
  3. Persist()
  4. ApplyChanges()

Answer: B) SaveChanges()

Explanation:

SaveChanges() detects tracked changes and sends the corresponding insert, update, or delete operations to the database. EF Core also provides SaveChangesAsync().

16. Which method adds a new entity to the context for insertion?

  1. Add()
  2. InsertEntity()
  3. CreateRow()
  4. NewEntity()

Answer: A) Add()

Explanation:

Add() begins tracking the supplied entity as a new entity. The actual database insert normally occurs when SaveChanges() is called.

17. Which method marks an existing entity for deletion?

  1. Remove()
  2. DeleteEntity()
  3. Drop()
  4. Erase()

Answer: A) Remove()

Explanation:

Remove() marks an entity as deleted in the context. The corresponding database delete is normally executed when SaveChanges() is called.

18. What does EF Core change tracking do?

  1. Tracks changes made to entity instances so they can be persisted
  2. Tracks only SQL Server log files
  3. Automatically creates backups of the database
  4. Tracks C# source-code changes

Answer: A) Tracks changes made to entity instances so they can be persisted

Explanation:

Each DbContext tracks entity instances and their states. When SaveChanges() is called, these tracked changes drive the corresponding database operations.

19. Which property provides access to the EF Core change-tracking system?

  1. EntityTracker
  2. ChangeTracker
  3. TrackManager
  4. StateManager

Answer: B) ChangeTracker

Explanation:

The ChangeTracker property on DbContext provides access to information and operations related to tracked entity instances.

20. Which option is commonly used to query entities without tracking them in EF Core?

  1. AsNoTracking()
  2. WithoutTracking()
  3. IgnoreTracking()
  4. ReadOnly()

Answer: A) AsNoTracking()

Explanation:

AsNoTracking() configures a query so that the returned entities are not tracked by the context. This is useful for read-only scenarios.

21. Which method is commonly used for eager loading of a related navigation property?

  1. Include()
  2. LoadRelation()
  3. JoinEntity()
  4. Fetch()

Answer: A) Include()

Explanation:

Include() is used to request related data as part of a query. EF Core also supports explicit loading and lazy loading in appropriate configurations.

22. Which method is commonly used to include a nested navigation property?

  1. ThenInclude()
  2. NestedInclude()
  3. IncludeChild()
  4. NextInclude()

Answer: A) ThenInclude()

Explanation:

ThenInclude() continues an include path to load additional related data further down a relationship chain.

23. Which type of relationship describes one principal entity associated with many dependent entities?

  1. One-to-one
  2. One-to-many
  3. Many-to-one only
  4. Zero-to-zero

Answer: B) One-to-many

Explanation:

A one-to-many relationship allows one principal entity to be related to multiple dependent entities, such as one blog having many posts.

24. In a typical one-to-many EF Core relationship, what does a foreign key represent?

  1. A value that identifies the related principal entity
  2. A temporary C# variable
  3. A database connection string
  4. A migration name

Answer: A) A value that identifies the related principal entity

Explanation:

A foreign key property on the dependent entity stores the key value that identifies its related principal entity.

25. Which EF Core API is used to configure a relationship using the Fluent API?

  1. HasRelation()
  2. HasOne() and HasMany()
  3. ConfigureRelation()
  4. LinkEntities()

Answer: B) HasOne() and HasMany()

Explanation:

EF Core's Fluent API provides relationship configuration methods such as HasOne(), HasMany(), WithOne(), and WithMany().

26. What is a navigation property used for in Entity Framework?

  1. To represent relationships between entity objects
  2. To store SQL connection strings
  3. To execute migrations automatically
  4. To configure Visual Studio navigation

Answer: A) To represent relationships between entity objects

Explanation:

Navigation properties allow entity classes to reference related entities or collections of related entities.

27. Which EF Core feature allows the database schema to evolve as the model changes?

  1. Entity caching
  2. Migrations
  3. LINQ projection
  4. Navigation properties

Answer: B) Migrations

Explanation:

EF Core migrations incrementally update the database schema to keep it synchronized with changes to the application's model while preserving existing data where possible.

28. Which .NET CLI command creates a new EF Core migration?

  1. dotnet ef migration create
  2. dotnet ef migrations add
  3. dotnet ef schema add
  4. dotnet ef database migration

Answer: B) dotnet ef migrations add

Explanation:

The dotnet ef migrations add command scaffolds a new migration containing the changes required to update the schema.

29. Which .NET CLI command applies pending EF Core migrations to the database?

  1. dotnet ef database update
  2. dotnet ef migrations run
  3. dotnet ef schema update
  4. dotnet ef database migrate

Answer: A) dotnet ef database update

Explanation:

dotnet ef database update applies the required pending migrations to the target database.

30. In Visual Studio's Package Manager Console, which command creates an EF Core migration?

  1. Create-Migration
  2. Add-Migration
  3. New-EFMigration
  4. Generate-Migration

Answer: B) Add-Migration

Explanation:

Add-Migration is the Package Manager Console command used to scaffold an EF Core migration.

31. What does the migration history table allow EF Core to determine?

  1. Which migrations have already been applied
  2. Which C# classes have compilation errors
  3. Which users logged into the application
  4. Which LINQ queries were executed

Answer: A) Which migrations have already been applied

Explanation:

EF Core records applied migrations in a special history table. This allows it to determine which migrations still need to be applied to the database.

32. Which method is commonly used to configure a SQL Server provider in EF Core?

  1. UseSqlServer()
  2. UseDatabaseServer()
  3. SqlServerProvider()
  4. ConnectSql()

Answer: A) UseSqlServer()

Explanation:

The SQL Server EF Core provider exposes UseSqlServer() for configuring a DbContext to use SQL Server.

33. Which method can configure the database provider inside a DbContext?

  1. OnConfiguring()
  2. ConfigureDatabaseOnly()
  3. InitializeProvider()
  4. SetupContext()

Answer: A) OnConfiguring()

Explanation:

A derived DbContext can override OnConfiguring() to configure database options, although applications commonly configure contexts through dependency injection as well.

34. Which EF Core method is used to configure a property as the primary key through the Fluent API?

  1. HasPrimaryKey()
  2. PrimaryKey()
  3. IsKey()
  4. SetKey()

Answer: A) HasPrimaryKey()

Explanation:

The Fluent API uses HasKey() on an entity configuration to define its primary key. For example, modelBuilder.Entity<Student>().HasKey(s => s.Id).

35. Which method is used to configure a property as required in EF Core's Fluent API?

  1. IsRequired()
  2. Required()
  3. MustExist()
  4. NotNull()

Answer: A) IsRequired()

Explanation:

IsRequired() configures a property as required, which generally maps to a non-nullable database column when the provider supports the corresponding relational behavior.

36. Which method configures the maximum length of a string property in EF Core's Fluent API?

  1. HasMaxLength()
  2. MaxLength()
  3. SetLength()
  4. LimitString()

Answer: A) HasMaxLength()

Explanation:

HasMaxLength() configures the maximum length of a property, such as a string property, when the provider maps that configuration to the database schema.

37. Which EF Core feature allows a query result to contain only selected properties rather than complete entities?

  1. Projection
  2. Migration
  3. Tracking
  4. Scaffolding

Answer: A) Projection

Explanation:

Projection uses LINQ methods such as Select() to shape query results into objects containing only the required data.

38. What does the following EF Core query return?

var names = context.Students
    .Where(s => s.Age >= 18)
    .Select(s => s.Name)
    .ToList();
  1. All student entities
  2. Only names of students whose age is at least 18
  3. Only students younger than 18
  4. The total number of students

Answer: B) Only names of students whose age is at least 18

Explanation:

Where() filters students to those aged 18 or older, while Select() projects each matching entity to its Name property. ToList() materializes the result.

39. Which LINQ method is used to count elements matching a condition?

  1. Count()
  2. Total()
  3. Size()
  4. NumberOf()

Answer: A) Count()

Explanation:

The LINQ Count() method can count all elements or elements satisfying a supplied predicate.

40. What is the purpose of AsQueryable() in a LINQ context?

  1. It converts a sequence to an IQueryable representation when supported
  2. It immediately saves all entities
  3. It disables LINQ completely
  4. It creates a database migration

Answer: A) It converts a sequence to an IQueryable representation when supported

Explanation:

AsQueryable() provides an IQueryable view of a sequence. In EF Core, queries originating from an EF DbSet are already queryable.

41. Which method can execute raw SQL that returns entity types in EF Core?

  1. FromSql()
  2. ExecuteEntitySql()
  3. RawEntity()
  4. SqlQueryOnly()

Answer: A) FromSql()

Explanation:

EF Core provides FromSql() and related APIs for composing queries that start from raw SQL, depending on the EF Core version and query scenario.

42. Which statement about EF Core LINQ queries is correct?

  1. Every LINQ query is always executed immediately
  2. Query execution commonly occurs when a terminal operation such as ToList() is reached
  3. LINQ queries can only return C# objects without database access
  4. EF Core cannot translate LINQ into SQL

Answer: B) Query execution commonly occurs when a terminal operation such as ToList() is reached

Explanation:

Many EF Core query-building operations are deferred. Operations such as ToList(), First(), and Single() trigger query execution.

43. What does the following code primarily do?

var blog = new Blog
{
    Url = "https://example.com"
};

context.Blogs.Add(blog);
context.SaveChanges();
  1. Deletes an existing blog
  2. Adds a new blog and persists it to the database
  3. Only reads an existing blog
  4. Creates an EF migration

Answer: B) Adds a new blog and persists it to the database

Explanation:

Add() marks the new entity for insertion, and SaveChanges() sends the pending insert operation to the database.

44. Which entity state normally represents a newly added entity before SaveChanges()?

  1. Unchanged
  2. Added
  3. Deleted
  4. Detached

Answer: B) Added

Explanation:

An entity passed to DbContext.Add() is normally tracked with the Added state. EF then generates an insert when changes are saved.

45. Which entity state indicates that an entity is currently tracked but has no detected changes?

  1. Unchanged
  2. Added
  3. Modified
  4. Deleted

Answer: A) Unchanged

Explanation:

The Unchanged state means the entity is being tracked and EF has not detected modifications that need to be persisted.

46. Which EF Core operation is designed to update many rows directly in the database without loading each entity into the change tracker?

  1. ExecuteUpdate()
  2. UpdateAllEntities()
  3. BulkSave()
  4. MassModify()

Answer: A) ExecuteUpdate()

Explanation:

ExecuteUpdate() allows an update operation to be executed directly against the database without first loading and tracking every affected entity.

47. Which EF Core operation is designed to delete multiple rows directly in the database without loading each entity?

  1. ExecuteDelete()
  2. DeleteAll()
  3. RemoveRangeDirect()
  4. BulkDeleteEntities()

Answer: A) ExecuteDelete()

Explanation:

ExecuteDelete() performs a database-side delete operation for the rows selected by the query without requiring the affected entities to be loaded into the change tracker.

48. Which statement best describes the recommended lifetime concept for a typical EF Core DbContext?

  1. It should normally represent a short-lived unit of work
  2. One instance should remain alive for the entire lifetime of the application
  3. It should always be stored in a static field
  4. It should never be disposed

Answer: A) It should normally represent a short-lived unit of work

Explanation:

DbContext is designed around a short-lived unit-of-work pattern. A context tracks entities and coordinates changes that are saved as part of that unit of work.

49. Consider the following query. Which entities are returned?

var products = context.Products
    .Where(p => p.Price > 1000)
    .OrderByDescending(p => p.Price)
    .ToList();
  1. All products in ascending price order
  2. Products priced above 1000, ordered from highest price to lowest
  3. Products priced below 1000, ordered from lowest price to highest
  4. Only the single most expensive product

Answer: B) Products priced above 1000, ordered from highest price to lowest

Explanation:

The Where() condition keeps products whose price is greater than 1000. OrderByDescending() then orders those products from highest price to lowest, and ToList() materializes the results.

50. Consider the following EF Core code. What is the most direct effect of AsNoTracking() in this query?

var products = context.Products
    .AsNoTracking()
    .Where(p => p.IsActive)
    .ToList();
  1. The query cannot access the database
  2. The returned product entities are not tracked by the context
  3. The query automatically updates the products
  4. The query deletes inactive products

Answer: B) The returned product entities are not tracked by the context

Explanation:

AsNoTracking() configures the query so the returned entities are not tracked by the DbContext. The Where() condition still filters the database results to active products. This pattern is commonly useful for read-only queries.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2026 www.includehelp.com. All rights reserved.