【问题标题】:SubSonic ASP.NET MVC sample in Visual Web Developer ExpressVisual Web Developer Express 中的 SubSonic ASP.NET MVC 示例
【发布时间】:2009-04-24 16:55:00
【问题描述】:

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

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

Cannot find custom tool 'TextTemplatingFileGenerator' on this system.

该工具保存在哪里? CodeTemplates 中有 .tt 文件,在您创建新的控制器或视图时使用,因此我假设有一个工具可以做到这一点。

【问题讨论】:

  • T4 模板是否适用于常规 MVC 项目?我在想 t4 模板可能没有安装 express?

标签: asp.net-mvc subsonic t4


【解决方案1】:

根据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 External Tools功能设置一个T4工具(Tools->External Tools): 设置这些属性:

  • 标题: T4
  • 命令: C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform.exe
  • 参数: $(ProjectDir)\Models\Classes.tt
  • 初始目录: $(ProjectDir)
  • Use Output windowPrompt for arguments应该被选中。

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

【讨论】:

  • 嘿,ranomore,我尝试了您的解决方案,但每次都会收到 Message Settings.tt(1,4) :警告:在模板中发现了多个模板指令。除第一个之外的所有内容都将被忽略。模板指令的多个参数应在一个模板指令中指定。你有什么想法吗?我查看了 Settings.tt 文件,只有一个模板指令。提前致谢
  • 我知道优秀的程序员会修复所有警告消息,但在这种情况下,例外是安全的。您看到的是外部文件中包含的某些文件也有一个模板指令。 T4 只处理它找到的第一个模板指令。
  • 我刚刚发现你把它捡起来并让它工作了,干得好。
  • 警告很烦人,使用 SubSonic 是否意味着在我的项目的剩余生命周期中不得不忍受这个警告?
  • 您可以尝试从 Settings.ttinclude 文件中删除模板指令。我认为这是造成悲伤的原因。
【解决方案2】:

事实证明,我不知道,T4 模板只能在 VS Standard 或更高版本上运行 :(。我曾一度认为它可以在 VS SDK 中使用 - 也许你可以找到它在那里:(

【讨论】:

    【解决方案3】:

    有一个可以使用的命令行 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 中运行,所以我很确定如果没有模板的至少一个补丁,您将无法让它们工作。

    【讨论】:

    • 不支持 Visual Studio 2008 Express Editions(根据系统要求)
    • 是的,安装程序会阻止您在没有标准或更高版本的情况下继续。但是我刚刚下载并安装了 express 并将 TextTransform 命令行工具添加到以下路径:C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2 所以希望你应该能够不使用 SDK。
    • 在 Classes.tt 上运行时出错:_SQLServer.tt(1,4):警告:在模板中发现了多个模板指令。除第一个之外的所有内容都将被忽略。模板指令的多个参数应在一个模板指令中指定。
    • _Settings.tt(1,4):警告:在模板中发现了多个模板指令。除第一个之外的所有内容都将被忽略。模板指令的多个参数应在一个模板指令中指定。 Context.tt(0,0):错误:运行转换:System.InvalidCastException:无法将“Microsoft.VisualStudio.TextTemplating.CommandLine.CommandLineHost”类型的对象转换为“System.IServiceProvider”类型。
    • 好的,现在我放弃了,不幸的是,模板希望在 Visual Studio 中运行并将 Host 转换为 IServiceProvider 以查找路径(Web.config 和 App_Data 目录)。您可能可以通过更改模板的执行方式来解决此问题,但似乎可能会有其他问题。抱歉,我无法提供更多帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2010-09-22
    • 1970-01-01
    • 2011-10-14
    相关资源
    最近更新 更多