We have a database project in Visual Studio 2013. In this project we have a .tt file which generates .sql script. The problem is after generation the build action of the generated file is automatically set to Build. If we change it manually to None, it gets reset to Build after regenerating (running custom tool).
Another strange thing is that it only happens if .tt file is in database project and in some folder of that project (not in root). if .tt file is in another project (anywhere) or in the root of the database project, the build action of the generated file does not change after regeneration.
We don't have any Visual Studio add-ins and I tried to disable all extensions and updates which could be disabled.
I will give you any details if needed.
How is the build action stored? In the csproj Note: Compile Generated Class file with a build action of Compile
Note: Compile generatedFile.cs with a build action of None
So now to set it to None.
Ok one T4 template that changes the sqlproj. Note : This is saving a new file for sqlproj instead of writing over your existing sqlproj as you will want to at a minimum :
- Backup your sqlproj
- Test on a new file
- Modify this to backup the sqlproj just in case.
- Have a extremely unique file name as the target.
- Have this as a separate T4 template
Run this after your custom tool
<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ Assembly Name="System.Core" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Collections.Generic" #> <# string file = @"C:\...\MyProject.sqlproj"; string [] lines = System.IO.File.ReadAllLines(file); //relative path of file you want to change build action string target = @"Models\GeneratedClass.cs"; //find the line that has your class for(int i = 0; i < lines.Length ; i++) { if(lines[i].Contains(target)) { //Replace Compile with None lines[i].Replace("Compile", "None"); } } string templateDirectory = Path.GetDirectoryName(Host.TemplateFile); string outputFileName = "newCSProj.sqlproj"; string outputFilePath = Path.Combine(templateDirectory, outputFileName); File.WriteAllLines(outputFilePath, lines); #>
Here is the warning Visual Studios shows before running a Text Template
Security Warning: Running this text template can potentially harm your computer. Do not run it if you obtained it from a untrusted source.
This answer was kind of mentioned in comments, but someone might miss it. Changed output extension to e.g. ".sqlscript" and default Build Action will be None.
<#@ output extension=".sqlscript" #>
You can also change default editor for this extension to
"Microsoft SQL Server Data Tools, T-SQL Editor"
so you can read and edit it as standard T-SQL script. Just go to
Tools -> Options.. -> Text Editor -> File Extensions.
It does not solve the problem with ".sql" files, but it is a good workaround and works fine for me. I tested it in VS2015 but it probably works similar in VS2013.
来源:https://stackoverflow.com/questions/30038839/generated-by-t4-file-build-action-gets-reset-to-build