Model First Approach in Entity Framework Asp.Net MVC 4 Example

In asp.net mvc Model First approach in entity framework is used to create Entities, relationships, and inheritance hierarchies directly on the design surface of EDMX, and it will generate a database from it. We will learn the entity framework model first approach with a simple example in asp.net mvc. 

 

Let's start with creating a new asp.net mvc 4 application for that Open visual studio à Go to File àSelect New àSelect Project.

 

create new asp.net mvc project from visual studio 2012

 

After that, you will see a new dialog for selecting your Template and Project type. From Templates, select Visual C# àinside that select Web and then project type select ASP.NET MVC 4 Web Application, and here we are giving the name as “MVCmodelfirst” then finally click on ok button.

 

select asp.net mvc 4 web application and click ok to create new mvc application

 

After naming it, click on OK now new dialog will pop up for selecting a template in that Select Empty template and click ok like as shown below. 

 

select template for model first entity framework asp.net mvc application

 

After completion of creating the application, we will add ADO.NET entity Model.

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Data Model, right-click on Model folder and select Add inside that Select ADO.NET Entity Data Model.

 

Adding ado.net entity data model in asp.net mvc model first applciation

 

After clicking on ADO.NET Entity Data Model, a New Dialog will pop up for entering Item name inside that you can enter any name, but it must be unique and click on the OK button like as shown below.

 

specify model name as employeemodel in asp.net mvc applicaiton

 

After that, a new Wizard will popup from that we need to select the second option, Empty model, and click on the Finish button like as shown below.

 

select empty model from entity data model wizard in asp.net mvc application

 

After adding our designer interface like the below image.

 

designer interface in asp.net mvc application

 

And our project view will be like as below image.

 

Model First Approach in entity framework asp.net mvc application structure

Adding Entity

For adding Entity, right-click inside the Entity Data Model Designer. A new dialog will pop up in that fill properties like as shown below.

 

Entity name: Add the name of the table which you want to create

Base type: None

Entity set: nothing to change here

Key Property (Primary key) Property name: EmpID

Property type: Int32

 

Entity Data Model Wizard with Database conneciton in asp.net mvc application

 

After adding, you will find your Entity as below image.

 

After adding entity model in asp.net mvc application

 

For adding more property, right-click inside Entity àSelect Add new option à Inside that select Scalar property like as shown below.

 

select scalar property from entity model in asp.net mvc application

Here is the complete Entity which we created.

 

Employee Details Table in Model First Approach in EF in asp.net mvc application

 

Now we need to generate a database from the above Entity. For that, right-click inside the Entity Data Model Designer and select Generate Database from Model like as shown below.

 

Generate database from model in model first entity framework approach in asp.net mvc

 

After clicking on Generate Database from Model, a new Wizard will popup.

 

Choosing New Database Connection in Model First Approach in Asp.net mvc

 

Now click on New Connection for configuring the Connection string. After clicking New Connection, a new dialog will pop up to enter all SQL server details.

 

And in place of Database, enter the name of the database you want to create (“MvcModelFirst”) and finally click on the ok button. A new dialog will pop up for Permission to create a Database. From that click, yes.

 

Create new database connection in model first approach in asp.net mvc

 

After clicking yes, a new wizard will pop up with the Connection string which you have entered. In this select Yes, Include sensitive data in the connection string and finally select Next Button.

 

newly created database in model first approach in asp.net mvc

 

After clicking on the Next button, a new wizard will pop up with the name Generate Database wizard.

 

Generate database wizard in model first approach in asp.net mvc

 

Finally, click on the Finish button. After adding, your project view will look as below image.

 

After generate database model in model first approach asp.net mvc application

 

Inside Employeemodel.edmx, you will find the Employeemodel.edmx.sql file. Open that file and right-click inside it, and select Execute.

 

execute script with right click in asp.net mvc application

 

After clicking on Execute it will open your SQL server login dialog.

 

connect database to execute script in model first asp.net mvc application

 

Enter your valid credentials of the SQL server. It will execute your script.

 

 

Finally, it has created a Database and Related table for you.

 

Output of Model First Approach in entity framework asp.net mvc application structure

 

This way, we can use the entity framework model first approach in asp.net mvc application.