Introduction To Entity Framework
Introduction To Entity Framework
Framework
Overview of Entity
Framework
Entity Framework (EF) is an object-
relational mapping (ORM)
framework for .NET applications. It
enables developers to work with
databases using .NET objects, which
simplifies data access and
manipulation. EF provides three
approaches for defining the data
model and working with databases:
Database First, Code First, and Model
First.
Features of Entity Framework
Entity Framework offers various features that enhance
data access and improve developer productivity
1. Automatic Code Generation: EF generates entity
classes and context based on the data model.
2. LINQ Support: EF allows using Language-Integrated
Query (LINQ) for querying data in a type-safe manner
3. Change Tracking: EF tracks changes to objects,
enabling easy updates to the database
4. Lazy Loading: Related entities are loaded
automatically when accessed for the first time
5. Eager Loading: Developers can specify which related
entities to load with the main entity
6. Migrations: EF supports database schema changes
without data loss
7. Transactions: EF supports transactions for data
integrity.
CRUD Workflow using entity framework
Main Tasks of EG API
1. Entity Data Model
2. Querying
3. Saving
Database First Approach
The Database First approach involves generating the data model (entity
classes) from an existing database. Developers can use the Visual Studio
Designer to create an Entity Data Model (EDM) by connecting to the
database and selecting the tables and views to include. EF then generates
the entity classes and a context class to perform CRUD operations.
Implementing Database First
To implement the Database First approach in C#, follow these steps:
1. Create a new Visual Studio project.
2. Add a new ADO.NET Entity Data Model.
3. Choose the 'EF Designer from Database' option and connect to the existing
database.
4. Select the tables and views to include in the model.
5. EF generates the entity classes and a context class to interact with the
database.
Code First Approach
In the Code First approach, developers define the data model (entity classes) in code
without an existing database. The model classes represent database tables and
relationships. EF automatically creates the database schema based on the defined
model when the application runs for the first time. Code First allows developers to
focus on the object model and later generate the database schema.
Implementing Code First
To implement the Code First approach in C#, follow these steps:
1. Create a new Visual Studio project.
2. Define the entity classes that represent the database tables and
relationships.
3. Configure the data model using EF Fluent API or Data Annotations.
4. EF creates the database schema based on the defined model during
the application's first run.
5. Optionally, you can enable migrations to handle future changes to the
data model.
Model First Approach
The Model First approach involves creating the data model using the Entity
Data Model Designer in Visual Studio. Developers design the entity classes
and relationships in the designer and then generate the database schema
and the context class. The Model First approach is suitable when the data
model is designed independently of an existing database.
Implementing Model First