65.9K
CodeProject is changing. Read more.
Home

MVC 6 Controller Scaffolding in Command Line

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (5 votes)

May 6, 2015

CPOL

1 min read

viewsIcon

34605

Controller Scaffolding in Visual Studio 2015 RC MVC 6 Project

Introduction

This tip discusses command line scaffolding in MVC 6 in Visual Studio 2015 RC.

Background

In this short tip, I will demonstrate how to do controller scaffolding in MVC 6 in the recent Visual Studio 2015 RC. CRUD scaffolding for controllers in MVC 6 in ASP.NET 5 has been moved to command line. In the latest RC of Visual Studio 2015, the .NET Execution Environment (DNX) has been renamed for k or kr. 

To start with the scaffolding process, please follow the article below to install DNVM to windows machine:

After installing dnvm, you can run from the command line you can run dnx command, for example to run the web site:

dnx . web

This will run the web site where the web command is defined in project.json file as below:

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls 
     http://localhost:5000"
}

Scaffolding Process

To create a new controller with CRUD operations can also be done with the dnx command from the prompt. To open the command prompt and go to your MVC 6 project folder where your project.json file is and apply the command to scaffold the new controller and the views as below:

dnx . gen controller -name NameOfController --dataContext DBContextName --model NameOfModel

Like web command, the gen command has also been defined in project.json as:

"commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "gen": "Microsoft.Framework.CodeGeneration"

  },

This is will create the controller and the views for CRUD operations.

In particular, I have used Person.cs model class and MyDBContext as the Database Context, the command was:

dnx . gen controller -name PersonController --dataContext MyDBContext --model Person

After running the command, it generates the controller and views as shown below:

Points of Interest

  • New feature of ASP.NET 5