根据Adam 的评论,您可以在 VS Express 中执行此操作,但正如 Adam 建议的那样,需要对模板进行更改。
Visual Studio 要求仅用于获取活动项目的路径,然后用于查找 web.config 文件和 app_data 路径。由于这些值在项目中通常是已知的,我们可以硬编码替代值
像这样更新 _Settings.tt 文件:
...
const string ConnectionStringName="Chinook";
//Use this when not building inside visual studio standard or higher
//make sure to include the trailing backslash!
const string ProjectPathDefault="c:\\path\\to\\project\\";
...
public EnvDTE.Project GetCurrentProject() {
if (Host is IServiceProvider)
{
IServiceProvider hostServiceProvider = (IServiceProvider)Host;
if (hostServiceProvider == null)
throw new Exception("Host property returned unexpected value (null)");
EnvDTE.DTE dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
if (dte == null)
throw new Exception("Unable to retrieve EnvDTE.DTE");
Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
if (activeSolutionProjects == null)
throw new Exception("DTE.ActiveSolutionProjects returned null");
EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
if (dteProject == null)
throw new Exception("DTE.ActiveSolutionProjects[0] returned null");
return dteProject;
}
return null;
}
...
public string GetConfigPath(){
EnvDTE.Project project = GetCurrentProject();
if (project != null)
{
foreach(EnvDTE.ProjectItem item in project.ProjectItems)
{
// if it is the configuration, then open it up
if(string.Compare(item.Name, "Web.config", true) == 0)
{
System.IO.FileInfo info =
new System.IO.FileInfo(project.FullName);
return info.Directory.FullName + "\\" + item.Name;
}
}
return "";
}
else
{
return ProjectPathDefault+"web.config";
}
}
public string GetDataDirectory(){
EnvDTE.Project project=GetCurrentProject();
if (project != null)
{
return System.IO.Path.GetDirectoryName(project.FileName)+"\\App_Data\\";
}
else
{
return ProjectPathDefault+"App_Data\\";
}
}
...
然后使用 VS External Tools 功能设置一个 T4 工具(Tools->External Tools):设置这些属性:
- 标题: T4
- 命令: C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform.exe
- 参数: $(ProjectDir)\Models\Classes.tt
- 初始目录: $(ProjectDir)
- 应检查使用输出窗口和提示参数。
单击“确定”,然后从“工具”->“外部工具”菜单中执行新创建的工具。