MVC中使用Scaffold生成代码
1、使用NuGET安装scaffold
Install-Package MvcScaffolding
2、Scaffold实现增删改查
创建实体类
namespace SoccerSite.Models { public class Team { public int TeamId { get; set; } [Required] public string Name { get; set; } public string City { get; set; } public DateTime Founded { get; set; } } }
3、使用如下命令生成增删改查相关文件
Scaffold Controller Team
创建过程
PM> Scaffold Controller Team Scaffolding TeamController... Added database context 'Models\Mvc4TestContext.cs' Added 'Team' to database context 'Mvc4Test.Models.Mvc4TestContext' Added controller Controllers\TeamController.cs Added Create view at 'Views\Team\Create.cshtml' Added Edit view at 'Views\Team\Edit.cshtml' Added Delete view at 'Views\Team\Delete.cshtml' Added Details view at 'Views\Team\Details.cshtml' Added Index view at 'Views\Team\Index.cshtml' Added _CreateOrEdit view at 'Views\Team\_CreateOrEdit.cshtml'
浏览器中输入下列网址http://localhost:17666/TEAM
4、Scaffolding a repository仓储模式
Scaffold Controller Team –Repository -Force
执行过程
PM> Scaffold Controller Team -Repository -Force Scaffolding TeamController... Mvc4TestContext already has a member called 'Team'. Skipping... Added repository 'Models\TeamRepository.cs' Added controller Controllers\TeamController.cs Added Create view at 'Views\Team\Create.cshtml' Added Edit view at 'Views\Team\Edit.cshtml' Added Delete view at 'Views\Team\Delete.cshtml' Added Details view at 'Views\Team\Details.cshtml' Added Index view at 'Views\Team\Index.cshtml' Added _CreateOrEdit view at 'Views\Team\_CreateOrEdit.cshtml'
生成文件
public interface ITeamRepository { void Add(Team post); void Delete(int id); IEnumerable<team> GetAllTeams(); Team GetById(int id); void Save(); }
What is a scaffolder?脚手架
在程序包管理器控制台中执行下列命令查看相关的命令
PM> Get-Scaffolder Name Description Package ---- ----------- ------- MvcScaffolding.Action Creates an action method, view model, and view MvcScaffolding 1.0.9 MvcScaffolding.ActionUnitTest Creates a unit test stub for an action method MvcScaffolding 1.0.9 MvcScaffolding.ActionWithUnitTest Creates an action method, view model, view, and unit test stub MvcScaffolding 1.0.9 MvcScaffolding.AspxView Adds an ASP.NET MVC view using the ASPX view engine MvcScaffolding 1.0.9 MvcScaffolding.Controller Adds an ASP.NET MVC controller with views and data access code MvcScaffolding 1.0.9 MvcScaffolding.RazorView Adds an ASP.NET MVC view using the Razor view engine MvcScaffolding 1.0.9 MvcScaffolding.Views Adds ASP.NET MVC views for Create/Read/Update/Delete/Index scena... MvcScaffolding 1.0.9 T4Scaffolding.EFDbContext Makes an EF DbContext able to persist models of a given type, cr... T4Scaffolding 1.0.8 T4Scaffolding.EFRepository Creates a repository T4Scaffolding 1.0.8 T4Scaffolding.CustomScaffolder Creates an entirely new scaffolder with a PS1 script and a T4 te... T4Scaffolding.Core 1.0.0 T4Scaffolding.CustomTemplate Allows you to modify the T4 template rendered by a scaffolder T4Scaffolding.Core 1.0.0
创建ASPX视图
Scaffold MvcScaffolding.AspxView Index Team
在程序包管理器控制台中输入‘-’按 “tab”弹出相应的参数
默认的脚手架What are “default scaffolders”?
PM> Get-DefaultScaffolder DefaultName ScaffolderName ----------- -------------- Action MvcScaffolding.Action Controller MvcScaffolding.Controller CustomScaffolder T4Scaffolding.CustomScaffolder CustomTemplate T4Scaffolding.CustomTemplate DbContext T4Scaffolding.EFDbContext Repository T4Scaffolding.EFRepository UnitTest MvcScaffolding.ActionUnitTest View MvcScaffolding.RazorView Views MvcScaffolding.Views
通过下列命令设置默认脚手架
Set-DefaultScaffolder View MvcScaffolding.AspxView
执行后显示如下信息
默认脚手架被改编成aspx视图
Using the built-in scaffolders使用内置的
Scaffold Controller <controllerNameOrModelType>
- - ControllerName:指定您需要的控制器的名称。根据录入的控制器名称寻找相应的model,创建控制器文件。
- - ModelType:默认情况下,我们试图推断出控制器的名称,但如果你想你的控制器和模型不具有相同的名字,你可以使用此参数指定一个特定的模型类型。我们会找到你的项目或引用另一个项目中定义的任何模型类,但不能是外部组件。
- - Project:通过此参数选择不同的项目。
- - CodeLanguage:生成语言C#或其他。
- - DbContextType:指定更新数据库上下文类的名称,例如,SoccerSiteContext。
- - Repository:这是一个开关参数,他不能带任何参数。只是增加存储库的命令行,意味着我们将生成一个存储库中的类和接口,控制器将数据访问使用该库的接口。
- - Area:如果你想进入一个特定的ASP.NET MVC生成的文件,在这里输入它的名称。请注意,您必须已经创建了该区域脚手架不自动创建区域。
- - Layout:使用特定的视图,输入虚拟路径,例如,“-Layout /Views/Shared/_SomeLayout.cshtml“。否则会使用默认的布局页。
- - Force:默认情况下,我们不覆盖已经 存在的文件。如果你想覆盖的东西,通过这个开关设置。
- - NoChildItems:如果由于某种原因,你只需要生成控制器类,不希望生成其他的东西,通过这个开关。
添加视图命令
Scaffold View <controllerName> <viewName>
- - Folder:强制输出到一个特定的文件夹相在项目中。默认情况下,我们使用/Model“文件夹。
- - Area:如果你还没有为文件夹设置一个值,这个参数填充文件夹使用 /Areas/<areaName>/Models。
- 其他的参数 –Project, –Force, and so on。
Scaffold Repository <modelType>
生成仓储接口(例如,IProductRepository)。
其他参数包括:
- - NoChildItems:不更新其他相关的内容。
- - Folder和- Area:。
- –Project, –Force, and so on。
重写scaffolders用LINQ to SQL
Install-Package LinqToSqlScaffolding
重写后默认信息如下
设置默认的生成方式
Set-DefaultScaffolder Repository LinqToSqlScaffolding.Repository