Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my T4 template append a number to the file name?

Why do my T4 templates sometimes append a number to the output file and sometimes not? For instance, in one case I might have a template file called Foo.tt and I'll get an output file of Foo.cs. In other cases, I'll get an output file of Foo1.cs. In every case, there is no other Foo.cs file that might be causing it to append a number. In other words, it is definitely not the result of any obvious file name conflict.

I'm a deeply anal retentive developer, so I'd sure love to know how to get rid of that useless numeric suffix.

like image 212
Gregory Higley Avatar asked Dec 21 '09 21:12

Gregory Higley


People also ask

What is a TT file?

TT stands for - Visual Studio Text Template is a software development tool created by the Microsoft. Further explanation - TT file contains text block and control logic used for generating new files. To write the Text Template file we can use either - Visual C# or Visual Basic Code.

What is T4 template 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

This happens when Visual Studio gets itself confused and briefly decides that it can't use Foo.cs as the output for some reason (usually hallucinatory), so it will use Foo1.cs instead, and then insists on remembering this setting.

The fix is to open the .csproj file in a text editor and locate the Foo.tt entry. This should have a sub-element called LastGenOutput. Change this back to Foo.cs, save the project file, and reopen it in VS.

And then -- sigh -- wait for it to happen again. You can see http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/0c0f77a6-d712-43d2-a990-555df7960123 for more details, though nobody seems to be able to explain what causes VS to get into this state or how to stop it doing so...

like image 86
itowlson Avatar answered Sep 20 '22 19:09

itowlson


@itowlson's answer really helped me out, but I discovered a slightly simpler workaround that I thought I'd share.

If you have:

Filename.tt └── Filename1.cs 

Just rename Filename.tt to Filename2.tt:

Filename2.tt └── Filename2.cs 

And back to Filename.tt again:

Filename.tt └── Filename.cs 

Voilà.

like image 20
RichardTowers Avatar answered Sep 23 '22 19:09

RichardTowers