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
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
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
After completion of creating application now we will add ADO.NET entity 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
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
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
After adding our designer interface like below image
And our project view will be like as below image
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
After adding you will find your Entity as below image
For adding more property just right click inside Entity àSelect Add new option à Inside that select Scalar property like as shown below
Here is complete Entity which we created
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
After clicking on Generate Database from Model a new Wizard will popup
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
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.
After clicking on Next button new wizard will pop up with name Generate Database wizard
Finally click on Finish button. After adding you project view will look as below image
Inside Employeemodel.edmx you will find Employeemodel.edmx.sql file just open that file and right click inside it and select Execute.
After clicking on Execute it will open your Sql server login dialog.
Just enter your valid credentials of Sql server it will execute your script.
Finally it has created Database and Related table for you.
This way we can use entity framework model first approach in asp.net mvc application.