T4 Include .cs 正在寻找 ITextTemplatingEngineHost

T4 Include .cs is looking for ITextTemplatingEngineHost

我的数据访问项目是 C# .Net 4.5。我添加了一个 ttinclude 文件,基于这个 post: http://erraticdev.blogspot.com/2011/01/generate-enum-of-database-lookup-table.html

我的文件 (EnumTypes.ttinclude) 如下所示:

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".generated.cs" #>
<#@ Assembly Name="EnvDTE" #>
<#@ Assembly Name="System.Data" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<# 
    string tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
    string path = Path.GetDirectoryName(Host.TemplateFile);
    string columnId = tableName + "ID";
    string columnName = tableName + "Name";
    string columnDescription = "Description";
    string connectionString = "Data Source=192.168.1.16;Initial Catalog=Mkp;Persist Security Info=True;User ID=sa;Password=1g1gl0b@l; MultipleActiveResultSets=true;Min Pool Size=20; Max Pool Size=200;Pooling=true;Connection Timeout=30";

    // Get containing project
    IServiceProvider serviceProvider = (IServiceProvider)Host;
    DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));
    Project project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject;
#>
using System;
using System.CodeDom.Compiler;
    ...

我的 IdentifierType.tt 文件应该使用如下所示:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ include file=".\EnumTypes.ttinclude" #>

生成的 EnumTypes.cs 文件有这些行:

private global::Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost hostValue;
public virtual global::Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost Host
{
    //...
}

但是,IDE 找不到 ITextTemplatingEngineHost。当我尝试添加对 'Microsoft.VisualStudio.TextTemplating.Interfaces' 的引用时,我在我的机器上找不到它。有我需要的 NuGet 包吗?

我正在尝试阅读有关 T4 的内容,以便了解正在发生的事情,但任何帮助都会很棒。

编辑: 通过在我的机器上浏览到 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll\',我能够修复 ITextTemplatingEngineHost 的 .cs 错误。

我现在收到错误消息:“名称 'Host' 在当前上下文中不存在”

回答: 我有错误的自定义工具。我需要将文件属性上的自定义工具设置为 'TextTemplatingFileGenerator'。

我用错了自定义工具。我需要将 tt 文件的文件属性上的自定义工具设置为 'TextTemplatingFileGenerator'。

我还删除了 .ttinclude 文件中的自定义工具设置。