Database First Approach In Entity Framework

Database first approach applies, when we create a database first or if we have an existing database.

framework

Steps of using database first approach in Entity framework

Step 1. Create a database or use an existing database in the DB first approach:

database

We have “SchoolDB”, which contains one table Student.

Step 2. Create Window Form Project.

Window Form Project

Step 3. Add “Ado.NET entity data model” inside the project.

Ado.net entity data model

Select “EF Designer from the database”.

EF Designer from database

Set the connection string.

connection

connection

connection

Select the database objects from the wizard.

Database objects

After finishing this wizard, we will get .edmx (StudentModel.edmx) file inside our project.

StudentModel

Step 4. StudentModel.edmx contains two .tt extension files, namely:
” StudentModel.Context.tt” and “StudentModel.tt”
” StudentModel.Context.tt” : It contains dbContext.
“StudentModel.tt”: It contains models or entity class.

Step 5. Now, if we expand the StudentModel.edmx, we get following:

StudentModel

SchoolDBEntity1 is the DbContext exists inside StudentModel.context.cs.

Step 6. Create UI for inserting the data into Student table by using DbContext.

Create UI

Step 7. On Add button, click insert record into Student table of DB by using DbContext.

code

Ebook Download
View all
Learn
View all