Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 template adding assembly of existing project in solution

Tags:

c#

t4

Hi I need to add the assembly of an an existing project in my solution in my T4 Template file. The problem is that my T4 template is in a project called Project.WebApi and the class that I need in my T4 template is inside a project called Project.Common.WebApi.

I have tryed importing the namespace like this:

<#@ import namespace="Project.Common.WebApi.T4TemplateAttribute" #> 

But I get this error:

The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)

I have tryed adding the assembly like this:

<#@ assembly name="Project.Common.WebApi" #> 

And I got this error:

Compiling transformation: Metadata file 'Project.Common.WebApi' could not be found

My project that contains the T4Template (Project.WebApi) has a reference to the Project.Common.WebApi but from what I read T4Template does not use the references in the projects.

How can I solve this issues?

like image 850
aleczandru Avatar asked Jun 06 '13 12:06

aleczandru


People also ask

What is the purpose of using T4 templates?

Design-time T4 text templates let you generate program code and other files in your Visual Studio project. Typically, you write the templates so that they vary the code that they generate according to data from a model. A model is a file or database that contains key information about your application's requirements.

What is transform all T4 templates?

t4 is basically a tool built into VS for doing text transformation, typically for doing code generation. Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.

How do I add a .TT file to Visual Studio?

Include the file into your Visual Studio project. In Solution Explorer, on the shortcut menu of the project, choose Add > Existing Item. Set the file's Custom Tools property to TextTemplatingFilePreprocessor. In Solution Explorer, on the shortcut menu of the file, choose Properties.

What are T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.


2 Answers

T4 works nearly completely independent from the rest of your code. You're on the right track though, with the assembly directive, but you'll need to specify a full path to the actual DLL of the assembly, unless the assembly is in the GAC (which it probably isn't).

Luckily, however, you can use MSBuild macros in T4 directives. So, you'll probably get something like

<#@ assembly name="$(SolutionDir)Project.Common.WebApi\bin\Debug\Project.Common.WebApi.dll" #> 

See MSDN for some more background on this syntax.

You still also need the import namespace directive.

Finally, be wary of project build order. The project that contains your T4 template now depends on Project.Common.WebApi, so you'll need to make sure that Project.Common.WebApi is built first. Otherwise, your T4 template might accidentally link to an older version of the assembly, making bugs really hard to track.

If you already have a project reference to it, you're all done, but otherwise you need to set up the dependencies correctly. You can do this in Visual Studio via the "Project Dependencies..." dialog. Right-click the project to find it.

like image 101
skrebbel Avatar answered Oct 07 '22 10:10

skrebbel


In case your project with T4 template has direct reference to the project, you can use $(TargetDir)

<#@ assembly name="$(TargetDir)Project.Common.WebApi.dll" #>

like image 26
czlatea Avatar answered Oct 07 '22 12:10

czlatea