T4 Include .cs正在寻找ITextTemplatingEngineHost

时间:2015-06-23 14:16:41

标签: c# t4

我的数据访问项目是C#.Net 4.5。我已根据此帖添加了一个ttinclude文件: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&#39;的引用时。我无法在我的机器上找到它。我需要一个NuGet包吗?

我试图阅读T4,以便了解发生了什么,但任何帮助都会很棒。

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

我现在收到错误:&#39;名称&#39;主持人&#39;在当前上下文中不存在&#39;

ANSWERED: 我的错误自定义工具。我需要将文件属性上的自定义工具设置为&#39; TextTemplatingFileGenerator&#39;。

1 个答案:

答案 0 :(得分:0)

我的自定义工具有误。我需要将tt文件的文件属性上的自定义工具设置为&#39; TextTemplatingFileGenerator&#39;。

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

相关问题