Generated (by T4) file Build Action gets reset to Build

时光毁灭记忆、已成空白 提交于 2019-12-03 01:23:05

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 :

  1. Backup your sqlproj
  2. Test on a new file
  3. Modify this to backup the sqlproj just in case.
  4. Have a extremely unique file name as the target.
  5. Have this as a separate T4 template
  6. 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.

Daniel Kozłowski

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!