Last K-MUG Session, there was a demo related to ASP.NET MVC scaffolding. This post is about scaffolding support in ASP.NET 5. This post uses K runtime for scaffolding, you can do it using Visual Studio 2015 Preview as well.
Modify project.json, add “Microsoft.Framework.CodeGenerators.Mvc”: “1.0.0-*” under dependencies.
Add “gen”: “Microsoft.Framework.CodeGeneration”, under commands
You are done with the configurations. Updated project.json file will look like this.
Execute KVM Upgarde command in command prompt, to upgarde your K Runtime.
Now execute KPM Restore command in command prompt. It will restore all the required nuget packages for scaffolding.
To scaffold the controllers and view, you need to execute the following command - “k gen controller -m TaskItem -dc TaskContext”, where TaskItem is the model class, and TaskContext is the DBContext. (As I am using SQLite, I have created DBContext class, if you are using SQL Server, you don’t require this, scaffolder will generate it for you.) Here is the TaskContext class.
Here is the generated output.
Customizing scaffolding templates.
Similar to previous versions of MVC, you can customize the templates by modifying the templates. In old versions of MVC, you can find the templates under “C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates” folder, but in ASP.NET 5, NuGet packages are stored on a per-user basis by default. So you can the scaffolding under following location - C:\Users<user>.kpm\packages\Microsoft.Framework.CodeGenerators.Mvc\1.0.0-beta1\Templates\, you can modify the contents of the files, and you can re-generate using -f (force) option.
k gen controller -f -m TaskItem -dc TaskContext
Happy Programming :)</user>
Did you like this article? Share it with your friends