Computer programming is getting smarter & smarter and the same way it’s been shrinking & layman readable. Let’s go to the first era of Computer science where the real geeks sit and code tons of lines for months just for moving an object on screen or for fetching an employee record from the database. But now the things are different; WE (Programmers/Developers) think it’s silly! Aren’t we?
Scaffolding blends with Entity framework and creates us the instance for the mapped entity model and writes down CRUD Operations code for us. That’s really charming and time saving that enhances developer’s productivity. Let’s get into real implementation action for some time.
I’m going to use Visual Studio 2012 express and ASP.NET MVC 4.
- Free ASP.NET MVC Online Test
- Online ASP.NET MVC Shopping Cart App
- Simple Steps to build ASP.NET MVC5 Application
- Understanding App Life Cycle in ASP.NET MVC
- ViewBag Vs ViewData Vs TempData Vs Session
- How to start HTML5 Mobile App Development?
- Mastering HTML5 Canvas
- ASP.NET MVC3 Vs MVC4 Vs MVC5 Vs MVC6
- AJAX with ASP.NET MVC Simplified
- A Practical Guide to ASP.NET Web API
Although when this ASP.NET MVC Tutorial was written, we used Visual Studio 2012 Express with with ASP.NET MVC 4 but Scaffolding concept is same if you are using new versions of tools and frameworks like Visual Studio 2013 | Visual Studio 2015 with ASP.NET MVC 5.
Microsoft always involved in creating things simple yet powerful. Starting from vanishing old black screen till Drag & Drop application development IDEs.
I do remember one of the Bill Gate’s quote
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.”
Ok enough stories, let me come to the point, so Microsoft again comes with a powerful feature called Scaffolding in the world of ASP.NET MVC programming.
Huge time saver for ASP.NET MVC developers
Scaffolding engine for ASP.NET MVC models helps web developers in relieving him from the tedious & mundane task of writing CRUD Operations codes. That is, connecting to a database table and perform often doing task such as Create, Retrieve, Update and Delete records.
Scaffolding engine uses Microsoft’s T4 templates to generate basic controllers and views for the models. Scaffolding blends with Entity framework and creates us the instance for the mapped entity model and writes down CRUD Operations code for us. That’s really charming and time saving that enhances developer’s productivity. Let’s get into real implementation action for some time. I’m going to use Visual Studio 2012 express and ASP.NET MVC 4.
Setting up the Platform
As a prerequisites, I do consider that you might know how to create a database and table using Entity framework. If no, please go through this well narrated tutorial (Creating First application with Entity framework) and come back.
- Open up Visual studio 2012 or later and create an ASP.NET MVC 4 application.
- Make sure we do have Entity Framework 5.0 or above in our project, which is automatically pulled in when you chose Basic while creating MVC new project. You don’t need to worry about if Entity Framework is not there. Nuget package console will help us getting it. You just need to type the command as marked in below figure and it will start the installation.
- Let us create a sample database/dbcontext using EntityFramework’s Code-First approach. We will create a Model class first as you can see the BookModel.cs in below figure.
- Create a new class called “BookDbContext” which inherits DbContext class and create DbSet property so that our EF will recognize that as an Entity. Please note the naming convention, here we have used plural for class Book.
- Now the next important thing is to Build our Project and make sure it compiles without an error, since only if the project gets built the Models will be instantiated and will be available for further reference.
Note: Microsoft Exam on ASP.NET MVC normally has lots of questions regarding CRUD Operations, so understanding the Scaffolding Engine code is really helpful. - Create a new controller called “BookController”, wizard shows you the option called “Scaffolding options” here comes the magic. It asks you for
- Template: Choose “MVC with read/write actions and views, using Entity Framework” as template. It will do the magic of creating code for all CRUD Operations/Actions and Views automatically.
- Model class: Entity needs to be mapped to a specific model class, here in our case, it’s BookModel.
- Data context class: Choose the DbContext class where the Book model is been referred and click on “Add”.
- You can see the “BookController” is auto populated with DbContext instantiation and basic Action Results. For example, Index() action which returns a View where it injects a list of Books fetched from the Entity Framework Database. If you look at code inside Index() method that fetches the list, it’s pretty simple and only one line.
- Scaffolding engine also creates us four major views required for CRUD such as show in the below image. For instance the view Create.cshtml generates codes such as referring to the model
@model WebDevelopmentHelp.Models.BookModel
And creates an html form and fields using Razor’s Html Helpers. - Alright, once we launch our application, we get 404 page since we don’t have Default/Index controller. Try post fixing the /book with the URL. As our DB is empty, an Empty Index view gets loaded.
- Let go ahead and create a Book entry by clicking on the Create New link and filling the form.
- Once the information on the form is submitted it will again be redirected to Index view by listing out the book information.
Note: Scaffolding only creates us the basic functionalities and we couldn’t rely completely on that alone. It helps in igniting the basic setup and we can enhance further to attain the full featured functionality.
Hope this article helps you learning what is Scaffolding in ASP.NET MVC and kindly let me know if you have question!
- 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.
More ASP.NET MVC and Entity Framework related Tutorials:
- ASP.NET FAQs (All you need to Know about ASP.NET MVC)
- All You Need to Know to Pass Exam: 70-486 (Developing ASP.NET MVC Web Applications)
- Entity Framework FAQs (More you need to know about Entity Framework)
- A Practical Guide to ASP.NET Web API [Download PDF | Web Edition]
- What’s New in C# 6.0 (Complete Picture of Evolution of C#)
- Understanding ASP.NET MVC Application Life Cycle
- MVC3 Vs MVC4 Vs MVC5 Vs MVC6
Top 10 Interview Questions and Answers Series:
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- a Must Have SharePoint Interview Questions