T4 change output of "FileName.tt" to "FileName1.cs" [duplicate] - c#

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.

Related

Build action is not supported

No code, since it's not a code problem, rather a visual studio one.
I've searched this problem up a bit and found people saying they had this problem, not really specifying how they got it, and most people answering "you should set AndroidManifest.xml's build actions to none" without any explanation of why this would work, which, well won't work for me since it's already what I have and I'm getting the error anyway.
How I came to have this problem:
I made a few files, file1, file2, file3, in my assets directory.
I would then read them with a streamreader. This worked.
Fast forward a few weeks, I decide to rename them, through visual studio, to file1.txt, file2.txt ... etc.
When I compiled/launched it on my device, it did not work. An exception on streamreader told me the file wasn't found. And before that I had 3 errors "#(Content)build action is not supported" for each of my files.
I assume the problem lies in the build actions or possibly the way android behaves with some specific files? Either way how could this have changed by changing the file format? I made them .txt files for clarity purposes, I didn't expect everything to break down after that.
Ontop of that, I could not revert it. I removed the file extensions, and I still have the same problem now.
What should I do to have my files in the assets folder properly deployed on my machine just like they used to ? Is it related to the build options?
Thanks in advance.
As highlighted by Jason in the comments above, the build actions for android assets should be AndroidAsset, which got changed after I renamed them. This menu can be found on the properties of the file.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/resources-in-android/android-assets?tabs=windows

Source file in CSC cannot be found

After a struggle with creating a database (first with an entity framework, after without), we have a database (and an EntityModel_HFFContext file we don't use, it's not connected). Normal work proceeded, and I installed a NuGet-package for an MVC sitemap.
From that moment, the project worked fine, but after a couple of days two warnings came up about two files missing: "The parent file, 'EntityModel_HFF.tt', for file 'locatie.cs' cannot be found in the project file." ("HFF" is the project name in here) and then those multiplied themselves into errors: "Source file 'locatie.cs' cannot be found" in a CSC file I cannot find.
These two files ('locatie.cs' and 'klant.cs') are not in any view, but somehow they are displayed outside all folders, saying they do not excist (see this screencapture). I have tried to delete those files manually, but that did not have any success. I have tried some other ways too, but that resulted into the complete deletion of the database, so now I am somewhat desperate.
I have no idea where to look and what to do, to get rid of these two files (or probably references to these files, since we don't use them at all), so I don't have these errors anymore. Has anyone of you have a clue of where I need to look (e.g. where this CSC file is) and what I need to adjust to fix this?
Maybe it can be useful to know I use Visual Studio 2013.
Update
I fixed the errors by unloading the project, editing the .csproj file and commenting out
<Compile Include="klant.cs">
<DependentUpon>EntityModel_HFF.tt</DependentUpon>
</Compile>
(and the other one too). Now these errors are gone - is this a safe way to do this?
The reason why you get the error is because when you deleted the locatie.cs and klant.cs file, you didn't remove them from the project solution itself, therefore, Visual Studio is expecting the file to be part of the project but is unable to find them, which explains the yellow warning signs.
Like what you said, all you have to do is to remove the files from the solution as they no longer exists. So yes, it is safe to comment out that portion in the .csproj file.
Although, I would like to mention that you didn't have to go to that much trouble, all you had to do was to delete the file in your solution in Visual Studio, since they no longer exists anyway.

rename class with file name in one step in Visual Studio

I am a long year Java programmer, but currently I code in C#. I am accustomed, that when I change filename, also the class name changes and vice versa. This doesn't work in my Visual Studio. I must rename the class name and file name separately, and it's sometimes annoying for me, because I always forget on it, so it results to that I have different class and file names.
Is there option in Visual Studio to put the renaming in one step?
Renaming a file works fine - it'll offer to change the code to match:
If you rename the class, then - indeed, this doesn't happen by default. There is no actual need for the two to match, note. But ultimately, rename is only an f2 away.
As of Visual Studio 2015 Preview 5, the "Quick Actions and Refactorings" context menu contains a "Rename file to [class].cs" command.
It seems to not work when the copied file is in the same folder and the class name is the same (which it will be if you just copied the file). I think the reasoning may be that rename doesn't descriminate between a class in a newly copied file and the original (See comment below by #xMichal).
The quick solution is to copy the file (I use ctrl+drag) into a nearby folder, rename, then move it back.
Another option (which may be Resharper specific), is to rename the class (and constructors) in the copied file, then use the tooltip to electively rename the file to match.
ReSharper has the MoveTypesIntoMatchingFiles menu option on the Refactor context menu, and it seems to do the trick. Just that if you have more than one class in the same file, they will be moved also into separate files, but that should be a good thing in most cases anyway.
Install the awesome visual studio plugin called Reshaper.
It does that and loads of other nice stuff.
Free for 30 days then its costs but is definitely worth it.
If you 'refactor' something, the name will be changed everywhere where that text or name has been used so you don't have to follow up changing the name every time its been used. However, you cannot do that with objects beyond the project eg: file name.

Two output file names resolved to the same output

Recently I created new Form called WorkersScreen. When I try to run the project I got this error:
Error 1 Two output file names resolved to the same output path:
"obj\x86\Debug\DryWash.WorkersScreen.resources"
What does it mean and how does one resolve it?
This can happen in the event of two .resx files pointing to the same form. Mostly happens when renaming forms (other reasons may apply, I'm not exactly sure)
If your particular form files looks like this:
Form1.cs
Form1.designer.cs
MyFormerFormName.resx
Form1.resx
then that usually implies that you renamed the form but Visual Studio didn't remove the old .resx file. Once you delete the file (in this example MyFormerFormName.resx) manually, the error should be gone upon next build.
Find Duplicates by Editing Project File
Note the name of the .resx failure.
Unload the project first by right clicking on your project then click Unload Project.
Right click your project again and click edit project.
It will show you some code, look (or Search within the file for the resx in step 1) for the duplicate values which in my case is look like this
<EmbeddedResource Include="frmTerminalSerial.resx">
<DependentUpon>frmTerminalSerial.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmVisual.resx">
<DependentUpon>frmVisual.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmVisual.resx">
<DependentUpon>frmVisual.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="My Project\Resources.resx">
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<SubType>Designer</SubType>
</EmbeddedResource>
Notice this portion, it is a duplicate!
<EmbeddedResource Include="frmVisual.resx">
<DependentUpon>frmVisual.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmVisual.resx">
<DependentUpon>frmVisual.vb</DependentUpon>
</EmbeddedResource>
Delete one of them so it will look like this
<EmbeddedResource Include="frmTerminalSerial.resx">
<DependentUpon>frmTerminalSerial.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmVisual.resx">
<DependentUpon>frmVisual.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="My Project\Resources.resx">
<Generator>VbMyResourcesResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.vb</LastGenOutput>
<CustomToolNamespace>My.Resources</CustomToolNamespace>
<SubType>Designer</SubType>
</EmbeddedResource>
Save it, right click to your project then click reload project. You are done!
Normally, if you create a migration like this
Add-Migration "UpdateProducts"
Visual Studio will create a migration with a class name like UpdateProducts. If you add a new migration later using the same migration name, it will generate a migration with a class name like UpdateProducts1., automatically adding an incremented digit to the end as a suffix. Every time you generate a new migration, the number goes up by one.
In our case, for some reason, VS got confused, and started generating subsequent migrations with the same class name as existing migration, so that that there were two auto generated migrations with the same name.
Simply changing the class name of the new migration clears the problem.
i did a little search
i had the same problem
it's probably beacause your form has two .resx
if you try to delete one, the problem will be gone
my form:
Form1.Designer.cs
Form1.resx
Log_in.resx
Form1
i deleted Log_in.resx and my program worked again
I just got this issue using VS 2019 community edition.
I didn't have duplicate .resx files. I didn't have duplicate .cs files.
I couldn't find the solution online.
To fix the problem, I cleaned the solution and then restarted VS.
Not sure why, but it worked. Hope this helps.
I had the issue and it was caused because I had a partial class of a custom control.
I had accidentally created a .resx file for the partial
I think I did this by hitting shift F7 on the partial class and producing the empty designer form.
Removing the .resx file on the partial class resolved it for me. I was using version control and it was showing that it was a new file.
Hope this helps
Make sure you don't have two .resx files. See in your project under YourServiceName.cs. Works for me.
This just happened to me. I had accidentally "Drag and dropped" a form into another. It ended up making a copy of it called "Copy of ". I deleted it and the problem went away.
This happened to me when I copied a form to reuse most of the functionality and then renamed the form. I went to the directory referenced, found that there was in fact only one file, copied the file in question, renamed it to reference the name of my new form, and then pasted the new file back in the directory.
I don't think this is a good practice (I'm new to C#) but it did work, instead of having to recreate everything. I also needed to update the form name in a few places throughout the solution, surprising in the original form as well as in the new form.
Copying forms does not seem like a good idea.
In my case, the issue was caused by an EmbeddedResource tag for a designer.resx file that somehow got added to the .csproj file.
Specifically, the following:
<EmbeddedResource Include="Forms\frmMenu.designer.resx">
<DependentUpon>frmMenu.designer.cs</DependentUpon>
</EmbeddedResource>
Search in all project the class reported in error list, like in this question case DryWash.WorkersScreen it could be repeated in another file with different file name but inside same class name.
I got this problem when I accidentally generated a migration with the same name as a business object.
The solution is to delete the migration and then create a new one with a different name.
In my case I had to to open the .csproj in Note++ and looked for .resx files in EmbeddedResource tags I found some strange .resx with strange language suffix example.aa.resx other files were normal files either with no language suffix or with *.ar suffix, I deleted the creepy embedded resource tag and that fixed the issue for me.
I had this problem today.
Some how the Form.Designer had an extension of .resx, for my solution it was TestForm.Designer.resx. There was already a TestForm.resx. I cleaned the solution, closed down VS, deleted the TestForm.Designer.resx file, and then restarted VS. I still had the error.
I then closed down VS and opened the .proj file using NotePad++ and found that the TestForm.Designer.resx was still referenced. I deleted the embedded resource.
<EmbeddedResource Include="TestForm.Designer.resx">
<DependentUpon>TestForm.Designer.vb</DependentUpon>
</EmbeddedResource>
Then I opened VS and built....It work's again!
This happened to me when I run Add Migration command and gave same name as my project name as migration class. I removed all migration classes and added new one, solution build again.
In my option, help delete one *.resx for every form where the error raised
Detail INFO
In our project we have 6 *.resx for every form for localization (DE,GB,SK,RU,SRB) and if i delete (from VS) FormName.sr-Latn-CS.resx than the error disappeared. If i try deleted FormName.en-GB.resx it did not help. Error disappeared just for delete sr-Latn-CS.resx (maybe a designer can not solved two - ). I first saw this error when I migrated project from VS 2010 Win 7 to VS2010 Win 10.
If this problem appeared while you were working with EntityFramework and was trying to Add-Migration, the solution is simple: delete Migrations folder (in the Solution Explorer) and execute Enable-Migrations. Backup migrations if you need.
For me the issue was copying and pasting a .aspx, and not renaming the code behind class files to match the name for the copied aspx file. Changing the code behind and designer class names worked.
I had the same issue, what I did was delete my migration files (via the studio), after I updated the database.
As #WimOmbelets stated in his 2013's answer, copying a Form is a very sure way to reproduce this annoying error, at least in VS 2017, 2019 and 2022.
As Vince De Giorgio said in his 2019's answer, I too didn't have duplicate .resx or .cs files and I didn't have duplicate ...EmbeddedResource Include= .../> tags in .csproj.
Deleting .resx files (icons gone, obvious) for both involved forms, clean the solution and then restart VS is a poor man's way to solve, because every time I add again an icon or another resource, same annoying error. It is still worse: to recover, I had to Git stash all modifications; if I revert manually all changes, to the point that Git does not recognize any changes, the error continues (!!!) even after cleaning, restarting VS and rebuilding.
To fix the problem in VS 2019 and 2022:
Every time that I want to copy a form, I:
Exclude Form to be copied from project (exclude, DO NOT DELETE!)
In Windows Explorer I create a Form's copy and open them (.cs and designer.cs) with Visual Code (whatever)
In Visual Studio I create Add a New Form with a different name - of course
Paste back designer.cs and .cs codes from VS code; change namespace, class name and Ctor
Include again in Project previous Form
Enjoy
Never more had this problem.
I had the same issue in Visual Studio 2022 with a WinForms application in .NET 6.
In this case, I had added a partial class in it's own .CS file to handle custom setup outside of the Designer-generated InitializeComponent.
The next time that I opened the file, Visual Studio 2022 tried to open this in the WinForm Designer. This added a redundant .resx file to the project.
Delete the newly added .resx to correct the error.
from the link:
https://learn.microsoft.com/en-us/visualstudio/msbuild/errors/msb3577?view=vs-2022
<EmbeddedResource Include="MyResources.resx">
<ManifestResourceName>CustomName</ManifestResourceName>
</EmbeddedResource>
open project file and ensure that the value given for CustomName is different for each generated resource file.
I thought that I would post on at least one random forum after encountering this error in hopes of easing someone elses problems. I searched many google sites and none had my exact problem. My cause was similar, but different in some ways.
I actually had a partial class spread across multiple files. this class was a form class. This was successful in itself and didn't cause a problem. However, this caused the new class to keep its unique file name while maintaining a split 'class Form' code. It was most likely not kosher practice, but none the less it happened at the time. It seemed like a good alternative to get a lot of cluttered code out of the way. Needless to say, next time I will just use regions or some other alternative.
The error then occured when I tried to take the code from the 2nd file and copy/paste into a new non form file with just a class, as I had intended upon essentially turning it into a library and making the code a little more proper and readable. As I didn't know exactly what had caused the error when it happened and had made a small plethora of changes, it took some time to track. Even after reversing the code, I managed to miss a pair of methods.
The methods for the main form containing the class Form with initializeComponent and form constructor (load). A 2nd copy of these methods appeared to result in the same error. Even after deleting the extra code and extra form and resx files. I even tried deleting the legitimate resx file since it was not actively needed. I was unable to effectively track it, because any errors pointed to the legit versions of these code segments. Ctrl+F and backup copies are your friends.
Hope that helps someone
I confirm WimOmbelets answer given in the comments. This can apparently happen when you rename a class in VS2012 (I never had this in a prior version).
I can't be sure this is the cause, but one thing I did different from my normal way is I used F2 to rename it as opposed to changing it and then pressing control-.
I had the same issue when I renamed one of my Form classes using Ctrl + R + R which VS provides to rename things by default.
Then I somewhat got two classes with the same class name on 2 different files (*.cs).
class Bar { ... } // From Bar.cs
class Bar { ... } // This should've been Foo, from Foo.cs
The two Bar indicating the same resource file that one of them shouldn't.
It's a trivial issue but sometimes could be hard to find the cause because looking at cs files just can't say which one is what you should look for.

SVN keeps corrupting files with "<<<<<<< .mine", how to fix?

I've got a Visual Studio C# project which is under version control (SVN).
I've always commited and updated the project without any problems. But a couple of hours ago Visual Studio throws the following error when I try to launch/rebuild the project:
Files has invalid value "<<<<<<<
.mine". Illegal characters in path.
I don't know how to fix this problem. What should I do?
That happens when svn encounters a conflict: You changed a file, the file on the server was changed and it cannot (easily) be merged automatically. You need to decide what is the correct solution now.
Subversion just adds the diff into your source file (and creates files next to it, called OriginalName.mine (unchanged) and OriginalName.rsomething (unchanged, server version)).
Fix the conflict and tell subversion that this is resolved.
just delete the obj folder and it will worked fine.
Remove the code that shouldn't be in the file throwing the error and remove the the three files with extensions .mine, .<somerevision> and .<some_other_revision>. svn updated files that now contain 'conflicts' and you need to resolve these conflicts by hand. Usually this means you edited a file, someone else edited the same file and checked in changes and you didn't pay attention when checking out the changed file.
Delete every thing you have in obj folder .
Remove your obj folder from svn version control . Because on every build it get updated and when other developer commit changes to solution SVN is unable to marge obj folder files and raise error
Files has invalid value "<<<<<<< .mine". Illegal characters in path.
Please read the Basic Usage chapter in the subversion book. It has a section about Merging conflicts by hand which explains the conflict markers you're seeing.
Removing the debug folders worked for me (see comment-not answer above).
I got this after moving 12 folders from one section of svn to a new section. So if you get this after moving a project and the error does not point to an actual file, this is likely your issue.
If you have AnkhSVN or VisualSVN installed resolving this is most likely as easy as right clicking the file in the solution explorer and selecting edit conflict.
This will open the changed file in your merge editor. (See Tools->Options->Source Control->Subversion User tools for AnkhSVN). With a good merge tool like the free to use SourceGear DiffMerge or TortoiseMerge, resolving the conflict is just a few mouseclicks away.
I have had this happen on a large scale where the files get marked resolved but the conflict metadata is still there. I wrote a regular expression for visual studio to find these, for instances where it is not feasible to simply revert the changed files.
http://www.codetunnel.com/blog/post/90/ever-merge-with-svn-and-mess-up-when-resolving-conflicts-read-on
I closed the IDE, then deleted the obj folder and restarted the IDE and rebuilt my Code. This worked for me.
Inside your project :
odj folder -> Debug -> project name.csproj.Filelistabsolute.txt(snb.csproj.Filelistabsolute.txt)
Inside the text file
>>>>>>>.mine and >>>>>>>.r150 occurs
to remove the things the program works
1)Just save your local changes whatever u have edited in the file
2)revert the file
3)update it from SVN
4)Paste your local changes
If you are using TortoiseSVN you should have a right click option on the file called Edit Conflicts. This should bring up TortoiseMerge which is able to read those obnoxious notations stuck into the file (really, to break your code so you KNOW there's an issue and don't blindly check it in).
TortoiseMerge will read it properly and present you with a 3-way merge. This was what I was looking for. Although it is true that it does also create the separate .mine and .rxxx and .ryyy files, and there are various manual and command-line ways to deal with all this.
Have a simple solution. just delete all file from debug folder and rebuild the solution, an error display on the screen "There were build errors. Would you like to continue and run the last successful build? click on "Yes" button. Now stop the program and run normally.

Categories

Resources