Add a using statement with an include template

Posts   
 
    
TopDog74
User
Posts: 40
Joined: 27-Apr-2012
# Posted on: 08-Jul-2015 11:23:26   

Hi could you show me how to add a using statement via an include template.

Basically i am trying to find the region 'AddtionalNamespaces'

and add the statement in there but it keeps just creating a new section called 'AdditionalNamespaces' at the bottom of the generated class file and putting the statement in there.

I have read the docs about templates, but to be honest i find it all rather confusing.

My template looks like this:

<%=DotNetTemplateEngine.GetUserCodeRegion("AdditionalNamespaces", null, GenerateUsing()) %>

<~ public string GenerateUsing() { return "using System.Linq;" + Environment.NewLine; } ~>

TopDog74
User
Posts: 40
Joined: 27-Apr-2012
# Posted on: 08-Jul-2015 14:27:48   

Actually decided to go with making a custom Entity Creator template instead. (Basically a copy of the existing EntityAdapter template with the using statement i wanted added in!)

I'd still like to know if what i was trying to do initially is possible? And if so, how would it be achieved?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 08-Jul-2015 20:39:36   

What you did is the way to go.

As for the original question:

<%=DotNetTemplateEngine.GetUserCodeRegion("AdditionalNamespaces", null, GenerateUsing()) %>

This should generate the using statement.

Why were you looking for "AdditionalNamespaces"? This is the code region where someone would add using statements at code time, not through the template system.

By the way for entities, you can use the Designer to add the additional namespaces from the Code Gen. sub tab in the entity Editor. You can also use a plugin for multi inserts.

TopDog74
User
Posts: 40
Joined: 27-Apr-2012
# Posted on: 10-Jul-2015 17:20:02   

Thanks for the Designer tip, i didn't realize i could do it that way.