跟着Adam's評論,你可以在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外部工具功能建立一個T4工具(工具 - >外部工具): 設置這些特性:
- 標題: T4
- 命令: C:\ Program Files文件\ Common Files文件\微軟 共享\ TextTemplating \ 1.2 \ TextTransform.exe
- 參數: $(PROJECTDIR)\型號\ Classes.tt
- 初始目錄: $(PROJECTDIR)
- 使用輸出窗口和提示參數應該被檢查。
單擊確定,然後從工具 - >外部工具菜單中執行新創建的工具。
T4模板是否適用於常規的MVC項目?我在想,t4模板可能沒有安裝express? – jfar 2009-04-24 17:11:50