Building your first MVC application with Entity Framework – Part 2

By | June 28, 2014
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

This is ASP.NET MVC 5 Tutorial Part-2 in series of building ASP.NET MVC Application using Entity Framework. In previous part, we built a simple ASP.NET MVC application (Product Catalog Application) and generate an EDM(Entity Data Model) following a step by step approach. We also got answers to:

  • What an ORM is?
  • Entity Framework as an ORM?
  • What is an EDM(Entity Data Model)?

In this part of ASP.NET MVC tutorial, we are going to generate views for our model classes. So, I’ll highly recommend to go through previous post for understanding about our EDM(Entity Data Model). Before we start implementation of generating views, let’s first understand a relevant concept i.e. Scaffolding.

What is Scaffolding in MVC?

In Software industry, Scaffolding is generally used as a way to generate some base code for a software that can be further customized or enhanced. So, Scaffolding in MVC is basically a code generation template that facilitate us to generate code for Controllers and Views etc.

You can find a comprehensive list of Entity Framework Interview Questions for beginners as well as experienced developers here.

For the purpose of this implementation, we will be using ASP.NET MVC scaffolding to automatically generate code for controller, it’s methods and related views against our model classes. Let’s take a look to our EDM(Entity Data Model) diagram:EDM Diagram

In Visual Studio 2015, right click on Controllers folder and choose “Add ->New Scaffolding Item” from context menu.ASP.NET MVC Scaffolding

Choose “MVC 5 Controller with views, using Entity Framework” option from Add Scaffold screen.

MVC Scaffolding

Choose appropriate values for Controller name, Model class, Data context class and options for Views as shown in following diagram and press “Add” button.ASP.NET MVC ControllerWe have given values as follows:

  • Controller name as “ProductController“.
    It will add a ProductController under Controllers folder.
  • Model class as “Product (MyProductCatalog.Models)“.
    Product class under MyProductCatalog.Models will be considered as Model to generate controller and views using MVC Scaffolding.
  • Data context class as “ProductCatalogEntities (MyProductCatalogModels)“.
    ProductCatalogEntities class under MyProductCatalogModels will be our Data context class.

Now, we can see that above given steps not only created “ProductController” under Controllers folder but also create “Product” folder under Views folder, containing views for all CRUD operations as:

  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml
  • Index.cshtml

ASP.NET MVC Scaffolding did a lot for us. We will go through the generated code for controller as well as view later in a separate post on this blog but let’s try to run the application now. In order to see the application running properly, right click on Index.cshtml and choose “View in Browser”.MVC View - Product
We can follow the same steps for creating all Controller and respective Views for our Category Model also. Finally, I’ll recommend to go through the generated code for our Controller and Views and try to make changes and see the output.

Previous << Part 1 – Building ASP.NET MVC Entity Data Model

Other ASP.NET MVC 5 and Related Tutorial:

Top 10 Interview Questions and Answers Series:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest
Category: ASP.NET MVC Entity Framework Tags: ,

About Web Development

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.