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

Why do my T4 templates sometimes add a number to the output file, and sometimes not? For example, in one case, I might have a template file called Foo.tt , and I get the output file Foo.cs In other cases, I will get the output file Foo1.cs In each case, there is another Foo.cs file, which can lead to the addition of a number. In other words, this is definitely not the result of any obvious file name conflict.

I am a deeply anal retentive developer, so I would really like to know how to get rid of this useless numerical suffix.

+41
visual-studio t4
Dec 21 '09 at 21:50
source share
2 answers

This happens when Visual Studio gets confused and for a short time decides that it cannot use Foo.cs as an output for some reason (usually hallucinatory), so instead it will use Foo1.cs, and then insists on remembering this setting .

The fix is ​​to open the .csproj file in a text editor and find the Foo.tt entry. This should have a subitem called LastGenOutput. Change this to Foo.cs, save the project file and open it again in VS.

And then - sigh - wait until it happens again. You can see http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/0c0f77a6-d712-43d2-a990-555df7960123 for more details, although no one seems to be able to explain why VS goes into this state or how to stop him doing it ...

+68
Dec 21 '09 at 22:36
source share
The answer to @itowlson really helped me, but I found a slightly simpler workaround that I thought I would share.

If you have:

 Filename.tt └── Filename1.cs 

Just rename Filename.tt to Filename2.tt :

 Filename2.tt └── Filename2.cs 

And go back to Filename.tt again:

 Filename.tt └── Filename.cs 

Voila.

+36
Jan 10 '13 at
source share



All Articles