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.Image may be NSFW.
Clik here to view.
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”.Image may be NSFW.
Clik here to view.
Choose Empty EF Designer model from Entity Data Model Wizard popup. Image may be NSFW.
Clik here to view.
Once we click on finish, we will get below screen:
Image may be NSFW.
Clik here to view.
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”.Image may be NSFW.
Clik here to view.
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:Image may be NSFW.
Clik here to view.
Further, we can add other properties of category as below:
Image may be NSFW.
Clik here to view.
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. :
Image may be NSFW.
Clik here to view.
Similarly, we can do this for Product entity.
Image may be NSFW.
Clik here to view.
Add association of Product with Category entity. It is done as :Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
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.Image may be NSFW.
Clik here to view.
Configuring Model Properties
Just click on the “Entity Data Model Designer” surface and open the properties window: Image may be NSFW.
Clik here to view.
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:Image may be NSFW.
Clik here to view.
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.
Image may be NSFW.
Clik here to view.
Put valid database details and click on “Test Connection”.Image may be NSFW.
Clik here to view.
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.Image may be NSFW.
Clik here to view.
This query generate the database for our app. It can be executed by the options as shown in below screen:Image may be NSFW.
Clik here to view.
We can check our database and will find following schema:Image may be NSFW.
Clik here to view.
Now create “CategoriesController” as shown in below image:Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Run the application and access “/category”, we will get following screen:
Image may be NSFW.
Clik here to view.
Create some categories, the category listing will appear as below:Image may be NSFW.
Clik here to view.
- 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.Image may be NSFW.
Clik here to view.
It will automatically generate the code for CRUD operations through scaffolding technique.
When we access “/Products”, following screen will appear:
Image may be NSFW.
Clik here to view.
Click on create new link, following screen will appear:Image may be NSFW.
Clik here to view.
We can find all categories will appear in category drop down.Image may be NSFW.
Clik here to view.
After creation of products, we get the list of product as follows:Image may be NSFW.
Clik here to view.
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
The post ASP.NET MVC Tutorial with EF6 Model First Approach appeared first on Web Development Tutorial.