生成(通过T4)文件构建操作将重置为Build

nig*_*der 22 .net c# t4 visual-studio

我们在Visual Studio 2013中有一个数据库项目.在这个项目中,我们有一个生成.sql脚本的.tt文件.问题是在生成之后,生成的文件的构建操作自动设置为Build.如果我们手动将其更改为None,则会在重新生成(运行自定义工具)后重置为Build.

另一个奇怪的事情是,只有当.tt文件在数据库项目中并且在该项目的某个文件夹中(不在root中)时才会发生.如果.tt文件位于另一个项目(任何地方)或数据库项目的根目录中,则生成文件的构建操作在重新生成后不会更改.

我们没有任何Visual Studio加载项,我尝试禁用可能被禁用的所有扩展和更新.

如果需要,我会给你任何细节.

llo*_*oyd 1

构建操作是如何存储的?在 csproj 中注意:使用编译操作编译生成的类文件

注意:使用 None 构建操作编译 generatedFile.cs

所以现在将其设置为 None。

确定一个更改 sqlproj 的 T4 模板。注意:这是为 sqlproj 保存一个新文件,而不是像您至少希望的那样覆盖现有的 sqlproj:

  1. 备份你的sqlproj
  2. 在新文件上测试
  3. 修改此以备份 sqlproj 以防万一。
  4. 有一个极其独特的文件名作为目标。
  5. 将其作为单独的 T4 模板
  6. 在您的自定义工具之后运行此命令

        <#@ 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); 
    
      #>
    
    Run Code Online (Sandbox Code Playgroud)

这是运行文本模板之前 Visual Studios 显示的警告

安全警告: 运行此文本模板可能会损害您的计算机。如果您从不受信任的来源获取它,请不要运行它。