Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 trouble compiling transformation

I can't figure this one out. Why doesn't T4 locate the IEnumerable type? I'm using Visual Studio 2010. And I just hope someone knows why?

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  #>
<#@ import namespace="System" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ output extension=".cs" #>
public static class Tables
{
    <#

    var q = @"
        SELECT 
            tbl.name 'table', 
            col.name 'column' 
        FROM 
            sys.tables tbl
        INNER JOIN 
            sys.columns col ON col.object_id = tbl.object_id
    ";

    // var source = Execute(q);

    #>
}
<#+
    static IEnumerable Execute(string cmdText)
    {
        using (var conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=t4build;Integrated Security=True;"))
        {
            conn.Open();

            var cmd = new SqlCommand(cmdText, conn);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                }
            }
        }
    }
#>

Error 2 Compiling transformation: The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?) c:\Projects\T4BuildApp\T4BuildApp\TextTemplate1.tt 26 9

like image 590
John Leidegren Avatar asked May 09 '10 19:05

John Leidegren


People also ask

How do I debug my T4?

To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.

What is T4 in Entity Framework?

t4 template is used to scaffold a DbContext class for the database, and the EntityType. t4 template is used to scaffold entity type classes for each table and view in the database.

What is T4 template and how it can useful?

T4 stands for Text template transformation toolkit is template based code generation engine. So you can write C# code in T4 templates ( . tt is the extension) files and those c# codes execute to generate the file as per the written C# logic.It is used in Entityfreamework, MVC to create strongly type views.


2 Answers

I would also recommend to referece #assembly name="System.Core" and #import "System.Linq" so you get more power when doing something with IEnumerable

like image 183
Tim Fischer Avatar answered Sep 23 '22 11:09

Tim Fischer


Probably because IEnumerable is in System.Collections.

like image 32
Femaref Avatar answered Sep 24 '22 11:09

Femaref