0% found this document useful (0 votes)
3 views14 pages

Entity Framework: Database, Code, Model First

The document provides an overview of Entity Framework (EF), an Object-Relational Mapping framework by Microsoft, and discusses three approaches to creating and maintaining an EF data model: Database First, Code First, and Model First. Each approach outlines the process of designing the database schema and generating corresponding application code. The document details the steps involved in each approach, emphasizing the flexibility and control offered by Code First and the visual design aspect of Model First.

Uploaded by

trail.drift0
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)
3 views14 pages

Entity Framework: Database, Code, Model First

The document provides an overview of Entity Framework (EF), an Object-Relational Mapping framework by Microsoft, and discusses three approaches to creating and maintaining an EF data model: Database First, Code First, and Model First. Each approach outlines the process of designing the database schema and generating corresponding application code. The document details the steps involved in each approach, emphasizing the flexibility and control offered by Code First and the visual design aspect of Model First.

Uploaded by

trail.drift0
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

Understanding Database

First, Code First, Model First

Presented BY
Bhuban Dhamala
Entity Framework (EF)
 Entity Framework (EF) is an Object-Relational Mapping
(ORM) framework developed by Microsoft.
 EF data model is a description of the data that your
application needs to work with.
 It is made up of entities, which are classes that
represent the data in your database.
 Each entity has properties that correspond to the
columns in the database table that it maps to.
1
 The EF data model also defines the relationships between
entities.
 For example, an entity might have a one-to-many relationship
with another entity, which means that one instance of the first
entity can be related to many instances of the second entity.

2
Understanding Database First, Code First,
Model First
 Database First, Code First, and Model First are three different
approaches to creating and maintaining an Entity Framework
(EF) data model.
 These approaches determine how the database schema and
the corresponding application code are generated, managed,
and maintained. Let's explore each approach in detail:

3
Database First:
 In the Database First approach, you start by designing the
database schema using a visual tool such as SQL Server
Management Studio or another database design tool.
 You first create a database schema, and then EF generates the
classes that represent the data in that schema.
 This code includes classes that represent database tables, and
often includes relationships between these classes.

4
Database First approach to creating an
Entity Framework data model:
 Create a new database in your database management system
(DBMS).
 Create the tables and columns in the database that you need.
 Create a connection string to the database.
 Use the Entity Framework Designer to create the classes that
represent the data in the database.
 The Entity Framework Designer will automatically map the
classes to the database tables.

5
 You can now start using your data model to query and update
the database.
 Here is an example of a Database First data model for a simple
application that tracks customers:

6
Code First:
 The Code First approach is a way of creating and managing a
database using Entity Framework (EF) by defining your data
model as object-oriented classes in your code, without having
an existing database schema.
 With Code First, you design your application's data model using
classes, and then EF generates the corresponding database
schema based on these classes.
 This approach is particularly useful when you want more control
over your data model

7
Define Entity Classes:
Create DbContext:
Code First approach to creating an Entity
Framework data model:
 Create a new project in your IDE.
 Add the Entity Framework NuGet package to your project.
 Create a class that represents each entity in your data model.
 Each entity class should have properties that correspond to the
columns in the database table that it maps to.
 You can use data annotations or fluent API to configure the mappings
and validations of your classes.

8
Cont…
 Create a DbContext class that inherits from DbContext.
 The DbContext class is responsible for managing the connection to
the database and the mapping between your classes and the
database tables.
 Create a migration file to create the database schema.
 Run the migration file to create the database.
 You can now start using your data model to query and update the
database.

9
Model First approach
The Model First approach to creating an Entity Framework data
model is a hybrid of the Database First and Code First
approaches.
In Model First, you first create a conceptual data model using the
Entity Framework designer.
The conceptual data model is a graphical representation of your
data model.
It shows the entities, relationships, and properties in your data
model.

10
Steps Involved in the Model First
Approach:
[Link] a new project in your IDE.
[Link] the Entity Framework NuGet package to your project.
[Link] the Entity Framework Designer.
[Link] and drop the entities, relationships, and properties from
the Data Sources window to the designer.
[Link] will generate the classes and the database schema for you.
[Link] can now start using your data model to query and update
the database.

11

You might also like