0% found this document useful (0 votes)
48 views5 pages

EF Interview Questions

The document contains a series of interview questions and answers related to Entity Framework, covering its components, approaches (Code First and Database First), and features like lazy loading and eager loading. It also explains various methods and concepts such as migrations, change tracking, and entity states. Additionally, it highlights differences between specific methods and versions of Entity Framework, providing a comprehensive overview for interview preparation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

EF Interview Questions

The document contains a series of interview questions and answers related to Entity Framework, covering its components, approaches (Code First and Database First), and features like lazy loading and eager loading. It also explains various methods and concepts such as migrations, change tracking, and entity states. Additionally, it highlights differences between specific methods and versions of Entity Framework, providing a comprehensive overview for interview preparation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Entity Framework Interview Questions

1. What is Entity Framework (EF)?

Answer: Entity Framework is an Object-Relational Mapping (ORM) framework in


.NET that enables developers to work with databases using .NET objects. It allows
you to interact with a database using strongly-typed entities and LINQ queries.

2. What are the main components of Entity Framework?

Answer: Entity Framework consists of three main components:


• Entity Data Model (EDM): A conceptual representation of the database
schema in terms of entities and their relationships.
• Object Services: Provides APIs to work with entity objects, such as
querying, saving, and tracking changes.
• Entity SQL: A query language for querying EDM models.

3. What is Code First in Entity Framework?


Answer: Code First is an approach in Entity Framework where you define your
data model using C# classes and conventions. The database schema is generated
from the code, and you can configure the mapping using attributes or fluent API.

4. What is Database First in Entity Framework?


Answer: Database First is an approach in Entity Framework where you create an
EDM from an existing database. The EDM is created from the database schema,
and you can then generate the classes and context for accessing the database.

5. Explain the difference between DbSet and DbContext in Entity Framework.


Answer: DbContext is the main class in Entity Framework that represents the
database session and provides access to database operations. DbSet is a property
on the DbContext that represents an entity set (a table) and allows you to query
and manipulate the data.

GAURAV SHARMA Bilal Noor


6. What is lazy loading in Entity Framework?
Answer: Lazy loading is a feature in Entity Framework that loads related entities
from the database only when they are accessed. This can help reduce the amount
of data loaded into memory but may lead to additional database queries if not
used carefully.

7. What is eager loading in Entity Framework?


Answer: Eager loading is the opposite of lazy loading. It loads related entities
along with the main entity in a single query, reducing the number of database
round-trips. You can use methods like .Include() to specify which related entities
to load eagerly.

8. Explain the different types of entity states in Entity Framework.


Answer: Entity Framework entities can be in various states:
• Unchanged: The entity hasn't been modified since it was queried.
• Added: The entity is not in the database but has been added to the
DbContext and will be inserted upon calling SaveChanges().
• Modified: The entity has been modified, and changes will be persisted
when calling SaveChanges().
• Deleted: The entity is marked for deletion and will be removed from the
database upon calling SaveChanges().

9. What is the purpose of migrations in Entity Framework?


Answer: Migrations are used to manage database schema changes in Entity
Framework. They allow you to evolve the database schema as your application
changes over time, creating and applying scripts for database updates.

10. Explain the difference between .AsNoTracking() and .AsTracking() in Entity


Framework queries.
Answer: .AsNoTracking() is used to retrieve entities without tracking them for
changes, making the query read-only. .AsTracking() is the default behavior, which
tracks the entities for changes and enables update operations.

GAURAV SHARMA Bilal Noor


11. What is the Entity Framework Core, and how does it differ from Entity
Framework 6?
Answer: Entity Framework Core (EF Core) is the lightweight and cross-platform
version of Entity Framework. It is a complete rewrite with a simplified API, better
performance, and support for .NET Core and .NET 5+.

12. Explain the difference between .FirstOrDefault() and .SingleOrDefault() in


Entity Framework queries.
Answer: .FirstOrDefault() returns the first element in a sequence or the default
value if the sequence is empty. .SingleOrDefault() returns the only element in a
sequence or the default value if there are no elements or throws an exception if
there is more than one element

.
13. What is the purpose of the .Include() method in Entity Framework?
Answer: The .Include() method is used to specify related entities to be included
in a query. It performs eager loading, allowing you to retrieve the main entity and
its related entities in a single query.

14. Explain the concept of change tracking in Entity Framework.


Answer: Change tracking in Entity Framework keeps track of changes made to
entities so that the context can generate SQL statements to update the database
appropriately. It allows Entity Framework to know which entities need to be
inserted, updated, or deleted when calling SaveChanges().

15. What is a complex type in Entity Framework?


Answer: A complex type is a type in Entity Framework that does not have an
identity and is not mapped to a specific table. Complex types are used to group
properties together in an entity and can be nested within other entities.

16. What is the purpose of a navigation property in Entity Framework?

GAURAV SHARMA Bilal Noor


Answer: A navigation property in Entity Framework represents a relationship
between two entities. It allows you to navigate from one entity to another
through the relationship, simplifying querying related data.

17. What are database indexes, and how can you create them using Entity
Framework?
Answer: Database indexes improve query performance. You can create indexes
in Entity Framework using the .HasIndex() method in a migration or code-first
configuration to specify which columns should be indexed.

18. Explain the difference between the .Add(), .Attach(), and .Update() methods in
Entity Framework.
Answer:
• .Add() is used to add a new entity to the context for insertion.
• .Attach() is used to attach an entity to the context to track changes.
• .Update() is used to mark an entity as modified for updates.

19. What is the purpose of the .Where() and .Select() methods in Entity
Framework queries?
Answer:
• .Where() is used to filter records based on a specified condition.
• .Select() is used to project the results to specific properties, transforming
the data before it is returned.

20. How do you handle transactions in Entity Framework?


Answer: Transactions in Entity Framework can be managed using the
TransactionScope class, which provides a way to wrap multiple database
operations in a single transaction and commit or rollback them as a unit.

DO Follow GAURAV SHARMA (https://www.linkedin.com/in/gaurav-vansul) for


interview preparation and more such questions.

GAURAV SHARMA Bilal Noor


GAURAV SHARMA

You might also like