In this ASP.NET MVC Tutorial, we are going to create a basic ASP.NET MVC application with Entity Framework 6 using Model First Approach. In our previous Tutorials on ASP.NET MVC, we created applications using EF 6 with Code First Approrach as well as Database First Approach. Also, we discussed all Entity Framework related concepts in detail in another Entity Framework Tutorial. We can follow below links to both of these Entity Framework Tutorials:
- MVC Application using Database First Approach
- MVC Application using Code First Approach
- MUST Have Entity Framework Interview Questions/FAQs
In the Model First approach, we create entities, relationships, and inheritance hierarchies directly on the design surface of EDMX and then generate database from our model.
So, Let’s start by creating a new ASP.NET MVC Application and adding new ADO.NET Entity Data Model as below. Right click on our DAL(Data Access Layer) folder, choose “Add” and then “ADO.NET Entity Data Model”.
Choose Empty EF Designer model from Entity Data Model Wizard popup.
Once we click on finish, we will get below screen:
We can create entities, association and inheritance between them on an empty designer by right clicking on designer surface. Choose “Add New” and then “Entity”.
EF 6 Navigation Property
Navigation property in Entity Framework 6 is a used to represent Foreign Key relationships between the entities just like Foreign Key relationship in database. Add Entity details as given in below screenshot, we are creating an Entity with name Category:
Further, we can add other properties of category as below:
Finally the category entity will look as below with Properties as CategoryId and CategoryName. For Navigation Properties i.e. used for Foreign Key relationship, we will see after creating all related entities. :
Similarly, we can do this for Product entity.
Add association of Product with Category entity. It is done as :
So, a One-to-Many association is developed between Category to Product. A single Category can have many Products associated, For example, Electronics Category will contain many electronic products like Mobile, Camera etc.
Configuring Model Properties
Just click on the “Entity Data Model Designer” surface and open the properties window:
Here we can modify the marked properties if we wish or keep the default.
Now, we need to generate database from model. Do it as:
Giving the Connection Properties of our database. Being a basic example, we are using Windows Authentication but for a professional project, we will provide proper properties and related authentication mode.
Put valid database details and click on “Test Connection”.
If this message appears, means your server is able to recognize and connect the database, otherwise there is any issue in the data we entered.
After completing all steps, we will get following screen.
This query generate the database for our app. It can be executed by the options as shown in below screen:
We can check our database and will find following schema:
Now create “CategoriesController” as shown in below image:
Run the application and access “/category”, we will get following screen:
Create some categories, the category listing will appear as below:
- Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer]
28 Lectures, 2.5 Hours Video, Intermediate Level
Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms. - AngularJS for ASP.NET MVC Developers [Brett Romero]
10 Lectures, 1 hour video, Intermediate Level
The Fastest Way For .NET Developers To Add AngularJS To Their Resume - ASP.NET with Entity Framework from Scratch
[Manzoor Ahmad, MCPD | MCT] 77 Lectures, 10 hour video, All Level
Latest approach of web application development - Comprehensive ASP.NET MVC [3D BUZZ]
34 lectures, 14 Hours Video, All Levels
From zero knowledge of ASP.NET to deploying a complete project to production.
Similarly we can add product controller.
It will automatically generate the code for CRUD operations through scaffolding technique.
When we access “/Products”, following screen will appear:
Click on create new link, following screen will appear:
We can find all categories will appear in category drop down.
After creation of products, we get the list of product as follows:
Hopefully, this basic example for ASP.NET MVC Application with Model First Approach using two related entities i.e. Category & Product will be helpful in practically understanding the concept.
- A. System Error
- B. It accepts new values
- C. Compiler error
- D. Throws ConstraintException
To further test your ASP.NET MVC/EF skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.
Further If you are going to appear for a Technical Interview on EF, you can get below a comprehensive list of Entity Framework Interview Questions and Detailed answers for beginners as well as advanced concepts.
- Entity Framework Interview Questions/Frequently Asked Questions (FAQs)
- ASP.NET MVC Interview Questions/Frequently Asked Questions (FAQs)
- What’s new in Entity Framework from beginning to EF7 – EF Core
- Bootstrap Interview Questions/Frequently Asked Questions (FAQs)
- AngularJS Interview Questions/Frequently Asked Questions (FAQs)
- jQuery Interview Questions/Frequently Asked Questions (FAQs)
Correct Answer: D