Entity Framework (EF) and EF Core MCQs ASP.NET

What is Entity Framework (EF)?
a. An Object-Relational Mapper (ORM) for .NET
b. A web framework for building APIs
c. A database management system
d. A language for defining database schemas

Answer: a. An Object-Relational Mapper (ORM) for .NET

What is the primary difference between EF and EF Core?
a. EF Core is cross-platform, while EF is not
b. EF supports only SQL Server, while EF Core supports multiple databases
c. EF is more efficient than EF Core
d. EF does not support migrations, while EF Core does

Answer: a. EF Core is cross-platform, while EF is not

Which method in EF Core is used to create a new database based on the model?
a. Database.EnsureCreated()
b. Database.Initialize()
c. Database.Create()
d. Database.Generate()

Answer: a. Database.EnsureCreated()

How can you handle database schema changes with Entity Framework Core?
a. By using migrations
b. By manually updating the database schema
c. By re-creating the database
d. By configuring schema changes in appsettings.json

Answer: a. By using migrations

Which EF Core class is used to configure the database context and model?
a. DbContext
b. DbSet
c. ModelBuilder
d. EntityTypeConfiguration

Answer: a. DbContext

What is the purpose of DbSet<T> in EF Core?
a. To represent a collection of entities of type T
b. To execute SQL commands
c. To manage database connections
d. To handle transactions

Answer: a. To represent a collection of entities of type T

Which method in EF Core is used to execute raw SQL queries against the database?
a. FromSqlRaw()
b. ExecuteSqlRaw()
c. SqlQuery()
d. RunSql()

Answer: a. FromSqlRaw()

What is Eager Loading in EF Core?
a. Loading related entities in a single query
b. Loading related entities on demand
c. Loading related entities when accessed for the first time
d. Using lazy loading for performance

Answer: a. Loading related entities in a single query

Which method in EF Core is used to add a new entity to the context?
a. Add()
b. Insert()
c. Save()
d. Create()

Answer: a. Add()

How can you configure entity relationships in EF Core?
a. By using the OnModelCreating method in DbContext
b. By setting properties directly in entity classes
c. By using data annotations on entity classes
d. By defining relationships in appsettings.json

Answer: a. By using the OnModelCreating method in DbContext

What does ChangeTracker do in EF Core?
a. It tracks changes made to entities in the context
b. It tracks database schema changes
c. It tracks SQL query execution
d. It tracks database connections

Answer: a. It tracks changes made to entities in the context

Which method in EF Core is used to remove an entity from the context?
a. Remove()
b. Delete()
c. Discard()
d. Erase()

Answer: a. Remove()

What is Lazy Loading in EF Core?
a. Loading related entities only when they are accessed
b. Loading all related entities in a single query
c. Using eager loading for performance
d. Using explicit loading for related entities

Answer: a. Loading related entities only when they are accessed

How do you configure EF Core to use a specific database provider?
a. By using UseSqlServer() or other Use methods in DbContext
b. By setting the provider in appsettings.json
c. By configuring the provider in Startup.cs
d. By using attributes in entity classes

Answer: a. By using UseSqlServer() or other Use methods in DbContext

What is the purpose of DbSet<TEntity> in EF Core?
a. To represent a collection of entities of type TEntity
b. To define entity relationships
c. To configure database connections
d. To manage database transactions

Answer: a. To represent a collection of entities of type TEntity

How do you perform database migrations in EF Core?
a. By using the Add-Migration and Update-Database commands
b. By using the Create-Migration command
c. By manually updating the database schema
d. By configuring migrations in appsettings.json

Answer: a. By using the Add-Migration and Update-Database commands

Which class in EF Core provides access to a database context?
a. DbContext
b. DbSet
c. DbConnection
d. DbContextOptions

Answer: a. DbContext

What is the purpose of SaveChanges() method in EF Core?
a. To persist changes made to the context to the database
b. To create a new database
c. To configure the database connection
d. To execute a raw SQL query

Answer: a. To persist changes made to the context to the database

Which EF Core class is used to manage entity configurations and mappings?
a. ModelBuilder
b. DbSet
c. DbContext
d. EntityTypeConfiguration

Answer: a. ModelBuilder

What does Attach() method do in EF Core?
a. Attaches an existing entity to the context for tracking
b. Adds a new entity to the context
c. Removes an entity from the context
d. Detaches an entity from the context

Answer: a. Attaches an existing entity to the context for tracking

What is Global Query Filter in EF Core?
a. A filter applied globally to all queries on a model
b. A filter applied to a specific query
c. A filter used for eager loading
d. A filter used for lazy loading

Answer: a. A filter applied globally to all queries on a model

Which method in EF Core is used to execute a raw SQL command that does not return data?
a. ExecuteSqlRaw()
b. ExecuteNonQuery()
c. RunSql()
d. ExecuteSqlCommand()

Answer: a. ExecuteSqlRaw()

How do you configure a composite key in EF Core?
a. By using the HasKey method in OnModelCreating
b. By using data annotations in the entity class
c. By setting the key in appsettings.json
d. By configuring the key in DbContext

Answer: a. By using the HasKey method in OnModelCreating

What does Entry() method of DbContext do?
a. Provides access to an entity’s state and allows tracking
b. Provides access to the database schema
c. Executes a raw SQL query
d. Configures entity relationships

Answer: a. Provides access to an entity’s state and allows tracking

What is AsNoTracking() in EF Core?
a. Disables change tracking for read-only queries
b. Enables change tracking for updated entities
c. Tracks changes for entities in a query
d. Prevents entities from being read

Answer: a. Disables change tracking for read-only queries

Which EF Core method is used to exclude an entity from being tracked by the context?
a. AsNoTracking()
b. Attach()
c. Remove()
d. Detach()

Answer: a. AsNoTracking()

What is the purpose of OnConfiguring method in DbContext?
a. To configure the database connection and options
b. To set up entity relationships
c. To define global query filters
d. To manage entity state

Answer: a. To configure the database connection and options

How do you perform a bulk insert operation in EF Core?
a. By using BulkInsert extension methods from libraries like EFCore.BulkExtensions
b. By using SqlBulkCopy
c. By calling AddRange() followed by SaveChanges()
d. By executing a raw SQL INSERT command

Answer: a. By using BulkInsert extension methods from libraries like EFCore.BulkExtensions

What does Find() method in DbSet<T> do?
a. Searches for an entity with the specified primary key
b. Searches for an entity by its name
c. Searches for entities based on a condition
d. Finds entities in a related collection

Answer: a. Searches for an entity with the specified primary key

Which EF Core method is used to specify a table name for an entity?
a. ToTable()
b. SetTableName()
c. DefineTable()
d. TableName()

Answer: a. ToTable()

What does the HasOne() method in EF Core configure?
a. A one-to-one relationship between entities
b. A one-to-many relationship between entities
c. A many-to-one relationship between entities
d. A many-to-many relationship between entities

Answer: a. A one-to-one relationship between entities

Which EF Core feature allows you to query related entities with filters applied?
a. Include() with filtering
b. ThenInclude()
c. Select()
d. Where()

Answer: a. Include() with filtering

What is the purpose of DbContextOptions in EF Core?
a. To configure and provide options for DbContext
b. To manage database migrations
c. To handle entity state changes
d. To execute SQL commands

Answer: a. To configure and provide options for DbContext

How do you define a many-to-many relationship in EF Core?
a. By using the HasMany() and WithMany() methods in OnModelCreating
b. By defining foreign key properties in entity classes
c. By using data annotations to define relationships
d. By configuring relationships in appsettings.json

Answer: a. By using the HasMany() and WithMany() methods in OnModelCreating

What does the OnModelCreating method in DbContext do?
a. Configures the model and entity relationships
b. Configures the database connection
c. Manages entity state changes
d. Handles database migrations

Answer: a. Configures the model and entity relationships

Which method in EF Core is used to execute a SQL query that returns scalar values?
a. ExecuteScalar()
b. FromSqlRaw()
c. ExecuteSqlRaw()
d. Scalar()

Answer: a. ExecuteScalar()

What does Update() method in EF Core do?
a. Marks an entity as modified and saves changes to the database
b. Updates the database schema
c. Executes a raw SQL update command
d. Configures entity relationships

Answer: a. Marks an entity as modified and saves changes to the database

Which method is used to retrieve an entity that has been deleted from the database but not yet removed from the context?
a. GetDeletedEntity()
b. Find()
c. GetDeleted()
d. DeletedEntity()

Answer: b. Find()

What is SqlServerDbContextOptionsBuilder used for in EF Core?
a. To configure SQL Server-specific options for DbContext
b. To configure database schema
c. To handle entity state changes
d. To perform raw SQL queries

Answer: a. To configure SQL Server-specific options for DbContext

How do you use data annotations to configure entity properties in EF Core?
a. By adding attributes to entity properties
b. By using fluent API configurations
c. By configuring in appsettings.json
d. By defining in OnModelCreating

Answer: a. By adding attributes to entity properties

What is EntityTypeConfiguration used for in EF Core?
a. To configure entity types and relationships using fluent API
b. To define the entity schema in appsettings.json
c. To manage entity state changes
d. To perform raw SQL queries

Answer: a. To configure entity types and relationships using fluent API

How do you apply a Global Query Filter in EF Core?
a. By using ModelBuilder in OnModelCreating method
b. By using DbContextOptions
c. By using data annotations in entity classes
d. By setting filters in appsettings.json

Answer: a. By using ModelBuilder in OnModelCreating method

What is DbSet<TEntity> used for in EF Core?
a. To perform CRUD operations on entities of type TEntity
b. To configure entity relationships
c. To manage database connections
d. To handle migrations

Answer: a. To perform CRUD operations on entities of type TEntity

Which method in EF Core allows you to specify custom SQL for a specific operation?
a. FromSqlRaw()
b. ToSql()
c. ExecuteSql()
d. Sql()

Answer: a. FromSqlRaw()

What does the Attach() method in EF Core do?
a. Attaches an existing entity to the context without changing its state
b. Adds a new entity to the context
c. Removes an entity from the context
d. Updates an existing entity

Answer: a. Attaches an existing entity to the context without changing its state

What is the purpose of the HasForeignKey() method in EF Core?
a. To configure foreign key properties for an entity
b. To define primary key properties for an entity
c. To configure many-to-many relationships
d. To configure entity state

Answer: a. To configure foreign key properties for an entity

Which EF Core method is used to configure the default value for a property?
a. HasDefaultValue()
b. SetDefaultValue()
c. DefaultValue()
d. SetDefault()

Answer: a. HasDefaultValue()

How do you configure an entity property to be required in EF Core?
a. By using the IsRequired() method in OnModelCreating
b. By using the [Required] data annotation
c. By setting the property to be non-nullable
d. By configuring in appsettings.json

Answer: a. By using the IsRequired() method in OnModelCreating

What does the AsTracking() method in EF Core do?
a. Enables tracking for entities retrieved from a query
b. Disables tracking for entities retrieved from a query
c. Marks entities as modified
d. Marks entities as deleted

Answer: a. Enables tracking for entities retrieved from a query

What is the purpose of DbContextOptionsBuilder in EF Core?
a. To configure and build options for DbContext
b. To manage database connections
c. To handle entity state changes
d. To perform raw SQL queries

Answer: a. To configure and build options for DbContext

Which method in EF Core is used to execute a SQL query that returns a collection of entities?
a. FromSqlRaw()
b. ExecuteSql()
c. SqlQuery()
d. Query()

Answer: a. FromSqlRaw()

How can you include related entities in a query using EF Core?
a. By using the Include() method
b. By using Load() method
c. By using Select() method
d. By using ThenInclude() method

Answer: a. By using the Include() method

Which class in EF Core is responsible for managing entity states and changes?
a. ChangeTracker
b. DbContext
c. DbSet
d. ModelBuilder

Answer: a. ChangeTracker