Database first approach applies, when we create a database first or if we have an existing database.
Steps of using database first approach in Entity framework
Step 1. Create a database or use an existing database in the DB first approach:
We have “SchoolDB”, which contains one table Student.
Step 2. Create Window Form Project.
Step 3. Add “Ado.NET entity data model” inside the project.
Select “EF Designer from the database”.
Set the connection string.
Select the database objects from the wizard.
After finishing this wizard, we will get .edmx (StudentModel.edmx) file inside our project.
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:
SchoolDBEntity1 is the DbContext exists inside StudentModel.context.cs.
Step 6. Create UI for inserting the data into Student table by using DbContext.
Step 7. On Add button, click insert record into Student table of DB by using DbContext.