-
What is Entity Framework (EF)?
- Entity Framework is an ORM (Object-Relational Mapping) framework provided by Microsoft. It enables developers to work with databases using .NET objects and eliminates the need for most of the data-access code that developers usually need to write.
-
Explain the different approaches to working with Entity Framework.
- Entity Framework supports three different approaches:
- Database-First: Creates entity classes based on an existing database schema.
- Model-First: Allows creating entity classes and relationships visually in the EDMX designer.
- Code-First: Enables developers to define entities, relationships, and configurations using code.
-
What are the key components of Entity Framework Core?
- Entity Framework Core includes:
- DbContext: Represents a session with the database and a gateway to perform CRUD operations.
- DbSet: Represents a collection of entities of a particular type within the context.
- Entity: Represents a class that corresponds to a table in the database.
- LINQ to Entities: Allows querying entities using LINQ.
-
Explain Lazy Loading and Eager Loading in Entity Framework.
- Lazy Loading: Lazy loading is the technique where related entities are loaded from the database only when they're accessed for the first time. It's the default behavior in Entity Framework Core, and it helps in avoiding unnecessary database calls.
- Eager Loading: Eager loading is the technique where related entities are loaded along with the main entity using the
Include
method or using projection. It reduces the number of database round trips but might fetch unnecessary data.
-
What is the difference between Add, Attach, and Update methods in Entity Framework Core?
- Add: Used to add a new entity to the context. It changes the state of the entity to Added.
- Attach: Used to re-attach an existing entity to the context. It changes the state of the entity to Unchanged.
- Update: Used to update an existing entity in the context. It changes the state of the entity to Modified.
-
What is the purpose of migrations in Entity Framework Core?
- Migrations in Entity Framework Core allow developers to evolve the database schema over time in a structured way. They enable creating, updating, and reverting database schema changes, keeping the database schema in sync with the application's data model.
-
How does Entity Framework handle relationships between entities (one-to-one, one-to-many, many-to-many)?
- Entity Framework supports various types of relationships:
- One-to-One: One entity instance is related to exactly one instance of another entity.
- One-to-Many: One entity instance is related to multiple instances of another entity.
- Many-to-Many: Multiple instances of one entity are related to multiple instances of another entity using a joining table.
-
Explain the advantages and disadvantages of using Entity Framework.
- Advantages: Rapid development, reduced amount of boilerplate code, easy to maintain, supports various database providers, LINQ support for querying data.
- Disadvantages: Performance overhead, potential for generating inefficient queries, complexity in some advanced scenarios, learning curve.
-
What is the purpose of the DbContext class in Entity Framework?
- The
DbContext
class in Entity Framework represents the session with the database and provides a set of APIs to perform CRUD (Create, Read, Update, Delete) operations on entities. It also manages the entity objects during their lifecycle.
-
Explain how Entity Framework handles transactions.
- Entity Framework Core supports transactions through the
SaveChanges
method. Multiple changes within a single SaveChanges
call are made within a transaction. Additionally, you can explicitly manage transactions using Database.BeginTransaction
and Transaction.Commit
/Transaction.Rollback
methods.