Problems with the basic example

Jan 11, 2010 at 11:37 AM
Edited Jan 11, 2010 at 11:40 AM

Hi

I'm currently working through the basic T4 example here: http://www.olegsych.com/2008/09/t4-tutorial-creatating-your-first-code-generator/

Once thing I have come across when following this particular example is that it doesn't generate the template properly on my machine. I'm running VS2008 Standard and just downloaded the latest versions of T4 Editor (free edition) and T4 Templates.

The static output is fine, but when it comes to putting dynamic elements in (as per your example), the compiler complains that PushIdent() and PopIdent() do not exist, and that 'object does not contain a definition for InPrimaryKey' (and a similar message for DataType).

I know what the compiler errors mean, but have I missed a reference or import out somewhere?

Here is my template so far (modified to connect to my local DB):

<#@ template language="C#v3.5" #>
<#@ output extension="sql" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
Server s = new Server("HOMER");
s.ConnectionContext.LoginSecure = true;
s.ConnectionContext.Connect();

Database db = new Database(s, "SimonBailes");
Table table = new Table(db, "User");
table.Refresh();

s.ConnectionContext.Disconnect();

#>
create procedure <#= table.Name #>_Delete
<#
	PushIdent("\t");
	foreach(var column in table.Columns)
	{
		if(column.InPrimaryKey)
			WriteLine("@" + column.Name + " " + column.DataType.Name);
	}
	PopIdent();
#>
as
	DELETE FROM Products WHERE ProductId = @ProductId

Many thanks,

Steve Hobbs

 

 

Jan 11, 2010 at 1:32 PM
Do you have the t4 editor installed

On Jan 11, 2010, at 6:37 AM, "elkdanger" <notifications@codeplex.com<mailto:notifications@codeplex.com>> wrote:


From: elkdanger

Hi

I'm currently working through the basic T4 example here: <http://www.olegsych.com/2008/09/t4-tutorial-creatating-your-first-code-generator/> http://www.olegsych.com/2008/09/t4-tutorial-creatating-your-first-code-generator/

Once thing I have come across when following this particular example is that it doesn't generate the template properly on my machine. I'm running VS2008 Standard and just downloaded the latest versions of T4 Editor (free edition) and T4 Templates.

The static output is fine, but when it comes to putting dynamic elements in (as per your example), the compiler complains that PushIdent() and PopIdent() do not exist, and that 'object does not contain a definition for InPrimaryKey' (and a similar message for DataType).

I know what the compiler errors mean, but have I missed a reference or import out somewhere?

Here is my template so far (modified to connect to my local DB):



<#@ template language="C#v3.5" #>
<#@ output extension="sql" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
Server s = new Server("HOMER");
s.ConnectionContext.LoginSecure = true;
s.ConnectionContext.Connect();
Database db = new Database(s, "SimonBailes");
Table table = new Table(db, "User");
table.Refresh();
s.ConnectionContext.Disconnect();
#>
create procedure <#= table.Name<http://table.Name> #>_Delete
<#
PushIdent("\t");
foreach(var column in table.Columns)
{
if(column.InPrimaryKey)
WriteLine("@" + column.Name<http://column.Name> + " " + column.DataType.Name<http://column.DataType.Name>);
}
PopIdent();
#>
as
DELETE FROM Products WHERE ProductId = @ProductId



<#@ template language="C#v3.5" #>
<#@ output extension="sql" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#
Server s = new Server("HOMER");
s.ConnectionContext.LoginSecure = true;
s.ConnectionContext.Connect();

Database db = new Database(s, "SimonBailes");
Table table = new Table(db, "User");
table.Refresh();

s.ConnectionContext.Disconnect();

#>
create procedure <#= table.Name<http://table.Name> #>_Delete
<#
PushIdent("\t");
foreach(var column in table.Columns)
{
if(column.InPrimaryKey)
WriteLine("@" + column.Name<http://column.Name> + " " + column.DataType.Name<http://column.DataType.Name>);
}
PopIdent();
#>
as
DELETE FROM Products WHERE ProductId = @ProductId

Many thanks,

Steve Hobbs





Read the full discussion online<http://t4toolbox.codeplex.com/Thread/View.aspx?ThreadId=80412&ANCHOR#Post275629>.

To add a post to this discussion, reply to this email (<mailto:[email removed]?subject=[t4toolbox:80412]>[email removed]<mailto:[email removed]>)

To start a new discussion for this project, email <mailto:[email removed]> [email removed]<mailto:[email removed]>

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings<https://t4toolbox.codeplex.com/subscriptions/thread/project/edit> on codePlex.com<http://codePlex.com>.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at <http://codeplex.com> codeplex.com<http://codeplex.com>
Jan 11, 2010 at 1:38 PM

Yes I've installed the latest version of the T4 Editor free edition, as I mentioned.

Jan 11, 2010 at 2:24 PM
Edited Jan 11, 2010 at 3:26 PM

Hmmm.

That is odd then.

I have the latest T4Editor and T4ToolBox and VS.NET 2008 Standard and I do use those methods in my templates, which have been in production for 2+ months.

Odd indeed.

I am sorry; but, I do not have any further ideas on the matter at this time.

Good luck regardless.

Jan 11, 2010 at 3:24 PM
Edited Jan 11, 2010 at 3:27 PM

FWIW, I have an end-to-end working sample built and ready-for-download here...

http://mkamoski1.wordpress.com/2009/12/01/t4-t4-toolbox-linq-to-sql-entity-base-code-description/

...and it does use those push-indent and pop-indent in the templates...

...and it does run on my home and work computer...

...so maybe that will help you?

(Of course, it was built after I first went through Oleg's excellent tutorials and articles on the matter.)

Coordinator
Jan 26, 2010 at 11:52 AM

Steve,

I have seen T4 Editor reporting its own errors separately from the T4 engine itself, sometimes incorrectly. Let's try to determine where your errors are coming from. Try to transform the template without opening it in the T4 Editor. In other words, right-click the template in Solution Explorer and select "Run Custom Tool". If the error is still reported, it comes from the T4 engine itself. In that case, could you post the complete error text here by copying it from the Errors window in Visual Studio?

Oleg