我创建了MVC项目并向其添加了“Class1.cs”(在同一个项目中) Class1代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication2.Models;
namespace MvcApplication2
{
public class EntityAttribute
{
public string Name { get; set; }
}
public class Entity
{
public List<EntityAttribute> Attributes { get; set; }
public string Name { get; set; }
}
public class Class1
{
public static string getTable()
{
return "tbl";
}
}
}
我有文本模板文件.tt:
<#@ template debug="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="$(SolutionDir)\MvcApplication2\bin\MvcApplication2.dll" #>
<#@ import namespace="MvcApplication2" #>
<#
string s = Class1.getTable();
#>
当我在TextTemplate上运行CustomTool时,出现以下错误:
Error 2 Compiling transformation: Metadata file 'MvcApplication2.dll' could not be found C:\Users\Igor\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\TextTemplate1.tt 1 1 MvcApplication2
当我将“&lt;#@ assembly name =”MvcApplication2.dll“#&gt; ”更改为完整路径“&lt;#@ assembly name =”C:\ Users \ Igor \ Documents \ Visual Studio 2010 \ Projects \ MvcApplication2 \ MvcApplication2 \ bin \ MvcApplication2.dll“#&gt; ”
我收到以下错误:
Error 2 Compiling transformation: 'MvcApplication2.Class1' does not contain a definition for 'getTable' c:\Users\Igor\Documents\Visual Studio 2010\Projects\MvcApplication2\MvcApplication2\TextTemplate1.tt 8 20
我的错误在哪里? 谢谢
答案 0 :(得分:1)
我发现了问题。
关于第一个错误:当无法生成tt文件时,这被视为错误并且应用程序未被编译,“MvcApplication2.dll”分别不存在。
解决方案:评论文件和构建应用程序的所有内容。然后,取消注释tt文件中的所有内容并再次运行CustomTool。
关于第二个错误:如前所述,如果无法生成tt fie,则会将此视为错误,并且应用程序未编译,相应的所有更改都不会保存,并且因此它说某些类型/方法无法找到(它们在dll中不存在)
解决方案:像在解决方案中一样。
--- --- BUT
即使在运行CustomTool时没有发生错误,构建时也会出错,因为VisualStudio和T4在其中一个人已经使用它时想要使用dll。
解决方案:使用T4Toolbox并更改
<#@ assembly name="$(SolutionDir)\MvcApplication2\bin\MvcApplication2.dll" #>
到
<#@ VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="$(SolutionDir)\MvcApplication2\bin\MvcApplication2.Dll" #>
您可以阅读文章并在GEORGE MATHEW的网站here下载工具箱