Introduction

Both ASP.NET 5 and ASP.NET Core 1.0 are the same. Now, ASP.NET 5 is called ASP.NET Core 1.0, so in this article we will be using ASP.NET Core 1.0.
What is Scaffolding?
CRUD is very easy and simple using Scaffolding. Yes, Scaffolding will automatically generate code on the controller and view for performing our CRUD operation, by selecting our MVC Model and DBContext. It saves the developer time by eliminating the need to write a single line of code for creating CRUD pages. Scaffolding will use Model and our DBContext for generating automatic code for our CRUD operations. We will see in detail in this article how to add Scaffolding in our project for our Student Master CRUD.
Prerequisites
- Visual Studio 2015: You can download it from here.
- ASP.NET 5 /Core 1.0: Download ASP.NET 5 RC from this link https://get.asp.net/
Using the code
After installing both Visual Studio 2015 and ASP.NET 5, Click Start, then Programs, and select Visual Studio 2015 -- click Visual Studio 2015. Click New, then Project, select Web, and select ASP.NET Web Application. Enter your Project Name and click OK.

Select Web Application under ASP.NET 5 Template and click OK.

We will be using our SQL Server database for our CRUD operation. First we create a database named StudentsDB, and a table, StudentMaster. Here is the SQL script to create the database table and a sample record insert query in our table.
- USE MASTER
- GO
- -- 1) Check for the Database Exists .If the database is exist then drop and create new DB
- IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'StudentsDB' )
- DROP DATABASE StudentsDB
- GO
- CREATE DATABASE StudentsDB
- GO
- USE StudentsDB
- GO
- -- 1) //////////// StudentMasters
- IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'StudentMasters' )
- DROP TABLE StudentMasters
- GO
- CREATE TABLE [dbo].[StudentMasters](
- [StdID] INT IDENTITY PRIMARY KEY,
- [StdName] [varchar](100) NOT NULL,
- [Email] [varchar](100) NOT NULL,
- [Phone] [varchar](20) NOT NULL,
- [Address] [varchar](200) NOT NULL
- )
- -- insert sample data to Student Master table
- INSERT INTO [StudentMasters] ([StdName],[Email],[Phone],[Address])
- VALUES ('Shanu','[email protected]','01030550007','Madurai,India')
- INSERT INTO [StudentMasters] ([StdName],[Email],[Phone],[Address])
- VALUES ('Afraz','[email protected]','01030550006','Madurai,India')
- INSERT INTO [StudentMasters] ([StdName],[Email],[Phone],[Address])
- VALUES ('Afreen','[email protected]','01030550005','Madurai,India')
- select * from [StudentMasters]


- "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-MYASP.NET5DemoTest-afb3aac0-d181-4278-8436-cafeeb5a8dbf;Trusted_Connection=True;MultipleActiveResultSets=true"
- "ConnectionString": "Server=YourSQLSERVERNAME;Database=StudentsDB;user id=SQLID;password=SQLPWD;Trusted_Connection=True;MultipleActiveResultSets=true;"

Creating our Model
We can create a model by adding a new class file in our Model Folder.

Right click the Models folder and click Add New Item. Select Class and enter your class name as “StudentMasters.cs”


Add the header file using System.ComponentModel.DataAnnotations; and add all our table field names as property in this model class, as in the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.ComponentModel.DataAnnotations;
- namespace MYASP.NET5DemoTest.Models
- {
- public class StudentMasters
- {
- [Key]
- public int StdID { get; set; }
- [Required]
- [Display(Name = "Name")]
- public string StdName { get; set; }
- [Required]
- [Display(Name = "Email")]
- public string Email { get; set; }
- [Required]
- [Display(Name = "Phone")]
- public string Phone { get; set; }
- public string Address { get; set; }
- }
- }
Creating DbContext
Now we need to create a DBContext for our Entity Framework. Same as with Model Class; add a new class to our Models folder.
Right click the Models folder and click Add New Item. Select Class and enter your class name as “StudentMastersAppContext.cs”


Now, first we need to add the header file for Entity framework using Microsoft.Data.Entity;
Next inherit the DbContext to our class, and then create object for our DBContext like the below code.
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Data.Entity;
- namespace MYASP.NET5DemoTest.Models
- {
- public class StudentMastersAppContext : DbContext
- {
- public DbSet<StudentMasters> Students { get; set; }
- }
- }
Adding Entity Framework Service in Startup.cs
Next we need to add our Entity Framework service in Startup.cs. We can find the Startup.cs file from our solution explorer .


Now we can add one more DBContext for ourStudentMastersAppContext asin the below code.
- // Add Entity Framework
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext<StudentMastersAppContext>(options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- public void ConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- services.AddIdentity<ApplicationUser, IdentityRole>()
- .AddEntityFrameworkStores<ApplicationDbContext>()
- .AddDefaultTokenProviders();
- services.AddMvc();
- // Add Entity Framework
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext<StudentMastersAppContext>(options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- // Add application services.
- services.AddTransient<IEmailSender, AuthMessageSender>();
- services.AddTransient<ISmsSender, AuthMessageSender>();
- }
Adding Scaffolding:
For adding the Scaffolding Right click Controller folder and click Add -> new Scaffolding Item.


Model Class: In Model Class select our Model Class which we created as “StudentMasters”.
Data Context Class: In Data Context select our DBContext class which we created as “StudentMastersAppContext”




Yes, everything is finished now, and we need just run our application and Create/Edit/Delete and View Student Master details.
Add Student Menu:
Before that, we must create a new menu to see our Students page.
For adding a menu click Views Folder -> Open Shared Folder and Open layour.cshtml page.

In layout.cshtml file we can find the following code.
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li><a asp-controller="Home" asp-action="Index">Home</a></li>
- <li><a asp-controller="Home" asp-action="About">About</a></li>
- <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
- </ul>
- @await Html.PartialAsync("_LoginPartial")
- </div>
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li><a asp-controller="Home" asp-action="Index">Home</a></li>
- <li><a asp-controller="StudentMasters" asp-action="Index">Student</a></li>
- </ul>
- @await Html.PartialAsync("_LoginPartial")
- </div>
Yes, everything is completed now, and your simple Student CRUD using ASP.NET 5 is completed. Now press F5 and run the project -- you can see the output as in the following image.



Anand NavalePosted Oct 4, 2019, 7:01 AM
Nice Sir...!
Humayun Kabir MamunPosted Jun 18, 2016, 12:49 PM
Nice...
Kashif SohailPosted Feb 10, 2016, 11:01 AM
Again a nice article
Jayakumar VinayagamPosted Feb 9, 2016, 4:02 AM
Good work, I parted my DB access into dll project and used in asp.net got some issues.
Ankur MistryPosted Feb 8, 2016, 11:43 PM
Nice share
Shubham KumarPosted Feb 8, 2016, 4:27 AM
nice
Navratna PawalePosted Feb 8, 2016, 1:28 AM
nice.
Sibeesh VenuPosted Feb 8, 2016, 12:11 AM
Nice Share
Pankaj Kumar ChoudharyPosted Feb 7, 2016, 8:39 PM
Again A nice Article , Thanks For Sharing such a brilliant and Useful Information........
Shweta LodhaPosted Feb 7, 2016, 3:11 PM
Nice
Santhakumar MunuswamyPosted Feb 7, 2016, 1:21 PM
Good work. Keep it up
sreenivasa kPosted Feb 7, 2016, 10:24 AM
nice 1
Ganjo AliPosted Feb 6, 2016, 8:46 AM
Very good
Saillesh PawarPosted Feb 6, 2016, 7:54 AM
excellent