T4 template generating library with new name - t4

I have a .tt file that has been working for a while. Recently, when I make changes, it has begun building an output file with a 1 at the end of the file name. At first, this caused problems because, having output.cs and output1.cs, I had duplicates of all my classes. However, when I delete them both, running the custom tool creates the ...1 file. If I then change it, and run it again, I still get the ...1 file (with the changes). How do I get back to having the original file name without the suffix?
I am running VS 2017. I have tried stopping and starting the IDE, and I found my .suo file and deleted it, too.

Edit the csproj file and fix the LastGenOutput setting that is associated with the your tt file:
<None Include="GeneratedCode\YourTemplate.tt">
<LastGenOutput>YourFile.cs</LastGenOutput>
</None>

Related

error - One or more projects in the solution were not loaded correctly

In VisualStudio2010 I changed the name of the project and the name of the solution.
When I try to open the solution I get this error:
One or more projects in the solution were not loaded correctly.
Any idea why I get this error?And how to fix it?
Try opening the .csproj file in notepad and making sure all references and paths are correct.
In my situation, I had the GlobalSection duplicated in my solution.
I'm using TFS and sometimes it has trouble to merge configuration from multiple developers at the same time.
Open your Solution (.sln) in NotePad++ and look up for this tag and remove one from the duplicated.
GlobalSection(TeamFoundationVersionControl) = preSolution
You may need to readd the last projects to the solution though.
I got the same trouble when using VC2012. My steps for it are: Control Panel --> Program & Functions --> Right Click on VC2012 --> Select "Recover" --> restart Windows --> re-open the project. It works.
The trouble comes from the damage of VC2012 iteself.
Try to open the application as a website instead of project. In my case the project/application was created as website and when i was trying to open the solution it was giving me the error so i opened the application as a website and the application got loaded.
Check .sln and .csproj files to see if any unused references are added without knowledge
I got the same issue after the code merge. The issue was few closing XML tags were removed. Fix: Open the .csproj file in a notepad and correct the closing tags.
I had this problem, the path for the .csprog files in the solution some how changed, instead of \\server\folder it changed to \server\folder.
Try open the .sln file using the Notepad and check all the .csproj files path.

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.
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...
#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à.
I've discovered something in VS2019 that might explain one potential cause of the issue.
In the CSPROJ file, VS expects a TT file be included using <Content Include="Generator.tt"> tags. When adding a TT file to a project via Cut/Copy/Paste using the contextual menu items in the interface built into VS, VS may sometimes use the wrong XML tag, such as <None Update="Generator.tt">. This will be despite the fact that if you open the Property sheet for the TT file, it will show 'Content' as the build action.
Open the CSPROJ file, and if you change the <None> tag to <Content> and also the Update attribute to Include, then restart VS, the issue appears to go away.

How to undo Visual Studio's obsession with recreating the .designer.cs file of my DBML?

This is driving me nuts. I think it is because of some connection string mismatching across the app (using MVC) but Visual Studio constantly takes my .designer.cs file and recreates it adding a '1' at the end (or 2 if it already did this before argh!). My question is two-fold: Is there a way to stop VS from doing this? But more importantly: How can I manually set it back to the original .designer.cs or is that not possible? I try to delete the new one it created (because I keep getting duplicate definition build errors) and it won't automatically associate back with the original one in the DBML file structure in solution explorer.
Does anyone have a solution for this madness?
Not an ideal solution, but a workaround would be the following. In Visual Studio, go into the properties of the .dbml file and clear out the Custom Tool property. This will prevent it from running the auto-generator against the DBML and creating the designer files. Of course further changes to the DBML won't be reflected in the designer file.
As for why it's generating multiple designer files, that's an oddity. To manually associate a file under another, you need to modify the project file. Look for a line like
<Compile Include="file.designer.cs" />
and change it to
<Compile Include="file.designer.cs">
<DependentUpon>file.dbml</DependentUpon>
</Compile>

Visual Studio - Fixing the error "A file of that name is already open"

Occasionally (usually after having updated my .sln file in source control) I get a strange Visual Studio error wherein I'm unable to open some of my files. The files in question show up in the appropriate project, but trying to open them results in an error dialog saying "A file of that name is already open."
This is virtually identical to Why does it say "Project with that name already opened in the solution"?, except for files, not projects. The solution given there was does not fix this.
Visual Studio internally maintains a list of currently opened files, to avoid problems caused by opening files more than once. Any number of things (crashes, reboots, updating files in source control outside of VS) can cause this list to become corrupted.
In any case, the problem can be fixed by deleting the hidden Solution.suo file which is in the same directory as your Solution.sln file. This will cause you to lose your current workspace state (open files, window layout, etc.), but it won't have any other adverse affects on your solution.
This is a hidden file, so to see or delete it you either have to enable viewing hidden files in Explorer or use del /AH Solution.suo on the command-line.
Delete the hidden .suo file and edit the .csproj file to remove the lines below:
<SccProjectName>Svn</SccProjectName>
<SccLocalPath>Svn</SccLocalPath>
<SccAuxPath>Svn</SccAuxPath>
<SccProvider>SubversionScc</SccProvider>
Now, reopen the solution to solve the issue.
Do you have any linked files in the solution?
Visual Studio has an invariant that only a single file of a given path can be open at one time. This invariant is hit most often when you have a linked file in your project / solution and attempt to open both the original and one of it's linked references.
Open csproj file of the project and delete following lines:
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
These lines are most probably created due to project is added to visual svn i.e. when project/solution is added to source control project/solution files are updated to include source control integration info and these lines are added which causes issues.
Delete these lines and just reload your project (or solution), this should fix issue.

Copy always to output directory does not work

I've got a simple console project where I'm reading two Excel-files. The Excel-files are included in the project ("add existing item") and I've marked them with "Copy to Output Directory". However, they are not copied to the debug-directory when debugging/running the code.
I feel like I've forgotten something trivial. What do I need to do more?
In the file properties in Visual Studio, set:
Build action: None
Copy to output directory: Copy always
Changes to non-source code files don't cause a rebuild to occur - they aren't considered when the compiler does it's out of date checking.
Try forcing a complete rebuild by deleting your output directory completely (sometimes doing this from within Visual Studio isn't complete).
It may be that the files haven't been copied across because a full build hasn't been run.
None of this worked for my WPF project. You need to mark it Content + Copy Always.
Refer to this page for details on the different Visual Studio file properties.
Did you mark them as content?
I had an issue when some png files was renamed-excluded-added again to project. It seemed that VS2015 had lost tracking what to do with these files: although in VS the "Copy to output directory: Copy always" was set at the problematic files, CopyToOutputDirectory key was not present in csproj file. I had to change csproj manually from
<Content Include="xxx.png"/>
to this:
<Content Include="xxx.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Silly question but are you running in debug mode? I've made the same mistake and realised I was in release mode.
I put Content and Copy Always and it worked.
I just had this problem and for some reason choosing "Create application without a manifest" under the project's properties finally copied the linked content file to the build directory.
VS 2015 behaves similarly, not updating the output directory correctly with Content files. What does seem to work, strangely, is to put a text file in the folder with the Content files and make it a Content file also. The text file will get copied to the directory and so will all the other Content files. Stranger still, if you then delete the text file, it will continue to show up in the output directory even though there is no longer an original to be copied.

Resources