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

  Total Views : 2494
  Comments : 0
 
  Keywords : What is model first in entity framework in asp.net mvc, Entity framework model first approach in asp.net mvc 4, Simple model first approach entity framework example in asp.net mvc

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 then it will generate database from it. We will learn entity framework model first approach with simple example in asp.net mvc. 

 

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

 

create new asp.net mvc project from visual studio 2012

 

After that you will see new dialog will pop up 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 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 just click on OK now new dialog will pop up for selecting 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 application now we will add ADO.NET entity Model

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Data Model just 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 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 second option Empty model and click on 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 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 just right click inside the Entity Data Model Designer a new dialog will pop up in that fill properties like as show below 

 

Entity name: Add name of 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 just 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 complete Entity which we created

 

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

 

Now we need to generate database from above Entity. For that just right click inside 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 Connection string. After clicking New Connection a new dialog will pop up in that enter all details related to Sql server.

 

And in place of Database enter name of database which you want to create (“MvcModelFirst”) and finally click on ok button then a new dialog will pop up for Permission for creating 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 Connection string which you have entered. In this select Yes, Include sensitive data in connection string and finally select Next Button.

 

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

 

After clicking on Next button new wizard will pop up with name Generate Database wizard

 

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

 

Finally click on Finish button. After adding you 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 Employeemodel.edmx.sql file just 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

 

Just enter your valid credentials of Sql server it will execute your script.

 

 

Finally it has created Database and Related table for you.

 

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

 

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User