The question is: is there a way to have a setting in the *.tt file so that the generated files are set to a specified Build Action?
The thing is I am generating SQL script using a template. I use this script as pre-deploy script in Database project. Currently, I have to manually set the Build Action to Pre-Deploy every time a file being re-generated - I would like to automate it.
Both files - template and SQL that it generates - are included into project. In Project (*.sqlproj) they are as next tags:
<PreDeploy Include="Migration\PreDeployScript.sql">
<DependentUpon>Migration\PreDeployScript.tt</DependentUpon>
</PreDeploy>
and
<None Include="Migration\PreDeployScript.tt">
<Generator>TextTemplatingFileGenerator</Generator>
</None>
When I run Custom Tool to re-generate SQL (I need to do it quite often), then PreDeploy tag is removed and Build tag is put on its place. Like this:
<Build Include="Migration\PreDeployScript.sql">
<DependentUpon>Migration\PreDeployScript.tt</DependentUpon>
</Build>
It breaks the project and I need to manually change back Build to PreDeploy.
Can specify something in Template file to preserve build action PreDeploy all the time?
Thank you!
You could use an item group with a wild card. This will make Visual Studio automatically include any file on disk relative to the project which matches the wildcard pattern.
This will only work (that is the files will only show when you load your project) if they exist on disk already.
This pattern will include any files with a g.sql suffix
<ItemGroup>
<Build Include="*\*.g.sql" />
</ItemGroup>
Unfortunately Visual Studio likes to expand these wildcards when you save the project, which effectively removes it. The best way to fix this issue is with indirection.
Create a new MSBuild project file with just the item group in it, then import the new project file with the <Import>
element in your SSDT project. The content from the included file will be merged into your primary SSDT project.
来源:https://stackoverflow.com/questions/36332947/how-to-set-build-action-for-generated-files-from-a-t4