T4 Include .cs is looking for ITextTemplatingEngineHost

1.8k views Asked by At

My data access project is C# .Net 4.5. I've added a ttinclude file, based off of this post: http://erraticdev.blogspot.com/2011/01/generate-enum-of-database-lookup-table.html

My file (EnumTypes.ttinclude) looks like this:

<#@ 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;
    ...

My IdentifierType.tt file which is supposed to use that looks like this:

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

The generated EnumTypes.cs file has these lines:

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

However, the IDE cannot find ITextTemplatingEngineHost. When I try to add a reference to 'Microsoft.VisualStudio.TextTemplating.Interfaces' I can't find it on my machine. Is there a NuGet package I need?

I'm trying to read up on T4s so I can understand what's happening, but any help would be great.

Edit: I was able to fix the .cs error for ITextTemplatingEngineHost by browsing to '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\' on my machine.

I now get the error: 'The name 'Host' does not exist in the current context'

ANSWERED: I had the wrong Custom Tool. I needed set the Custom Tool on the File Properties to 'TextTemplatingFileGenerator'.

1

There are 1 answers

0
M Kenyon II On BEST ANSWER

I had the wrong Custom Tool. I needed set the Custom Tool on the File Properties for the tt file to 'TextTemplatingFileGenerator'.

I also removed the Custom Tool settings on the .ttinclude file.