Here, we will learn about using DB First approach in ASP.NET MVC with entity framework. In DB first approach, Database is created first and from the database, POCO classes are generated automatically by Entity framework.

Firstly we will create the database in SQL Server.

Create the database

database-first-approach-with-asp-net-mvc-framework-1

Enter the database name

database-first-approach-with-asp-net-mvc-framework-2

Create the table.

database-first-approach-with-asp-net-mvc-framework-3

Enter the table fields and make Id column as Identity

database-first-approach-with-asp-net-mvc-framework-4

Enter the table name

database-first-approach-with-asp-net-mvc-framework-5

Open Visual Studio and create a new project.

Enter the project name and select ASP.NET Web Application

Choose MVC as a framework

Now we have to Install the Entity Framework for performing database related operations. So open the Package manager console.

database-first-approach-with-asp-net-mvc-framework-9

Write the command in the console

Install-Package EntityFramework

Add the ADO.NET Entity Data Model into the Models folder.

database-first-approach-with-asp-net-mvc-framework-11

Choose Data from Left Menu and choose ADO.NET Entity Data Model and give it a name.

Choose EF Designer From Database and Click next.

Select the Server and authentication types and database

Test the connection as it properly has done or not.

database-first-approach-with-asp-net-mvc-framework-15

Enter the Model name if you have to change.

database-first-approach-with-asp-net-mvc-framework-16

Choose the object you want to include in EDMX file.

database-first-approach-with-asp-net-mvc-framework-17

You will see the .edmx file

database-first-approach-with-asp-net-mvc-framework-18

Now your connection will be registered with your web application and you can see the connection string in Web.Config file present at the root folder.

database-first-approach-with-asp-net-mvc-framework-24

You will see that the Model is generated in the Models folder.

database-first-approach-with-asp-net-mvc-framework-19

Further, we will continue in Part-2