Visual Web Developer Express中的SubSonic ASP.NET MVC示例

SubSonic ASP.NET MVC sample in Visual Web Developer Express

在Visual Web Developer Express 2008中,SubSonic ASP.NET MVC模板似乎不适用于我添加的新数据库。我删除了Chinook数据库并创建了自己的数据库。我了解Models文件夹中的.tt文件用于生成代码,但它们不会(尽管将ConnectionStringName更改为我在web.config中设置的内容)

右键单击每个.tt文件并选择"运行自定义工具"不会生成任何内容,除了错误消息:

1
Cannot find custom tool 'TextTemplatingFileGenerator' on this system.

该工具存放在哪里? CodeTemplates中有.tt文件,当您创建新的控制器或视图时会使用它们。因此,我假设有一个工具可以执行此操作。


跟随亚当的评论,您可以在VS Express中执行此操作,但是模板需要做一些更改,如亚当建议的那样。

Visual Studio要求仅用于获取活动项目的路径,然后将其用于查找web.config文件和app_data路径。由于这些值在项目中通常是已知的,因此我们可以对替换值进行硬编码

像这样更新_Settings.tt文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
...
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 \\\\ Microsoft
    共享\\\\ TextTemplating \\\\ 1.2 \\\\ TextTransform.exe
  • 参数:$(ProjectDir)\\\\ Models \\\\ Classes.tt
  • 初始目录:$(ProjectDir)
  • 使用输出窗口和提示输入参数应进行检查。

单击"确定",然后从"工具"->"外部工具"菜单中执行新创建的工具。


事实证明,我不知道,T4模板只能在VS Standard或更高版本上运行:(。我曾经以为VS SDK可以同时使用它-也许您可以找到它在里面:(


有一个可以使用的命令行TextTransform工具:

[http://msdn.microsoft.com/en-us/library/bb126461.aspx] [1]

在Express版本中,默认情况下将其安装到C:\\\\ Program Files \\\\ Common Files \\\\ Microsoft Shared \\\\ TextTemplating \\\\ 1.2

但是,MVC模板要求t4模板必须在Visual Studio中运行,因此我敢肯定,如果没有对模板的补丁,您将无法使其正常工作。