简体   繁体   English

VS 中的 SQL Server 数据库项目。 构建过程中 sql 脚本生成步骤的顺序

[英]SQL Server database project in VS. Order of sql script generation step in build process

I have a SQL Sever database project in my solution and several dependencies projects.我的解决方案中有一个 SQL Sever 数据库项目和几个依赖项项目。

When I compiling (in VS) this database project I've get a SQL script and a .dacpac file as result.当我编译(在 VS 中)这个数据库项目时,我得到了一个 SQL 脚本和一个.dacpac文件。

But also I want to aggregate all my dependencies projects in one dll and make SQL script/ .dacpac file only for this result dll.但我也想将我所有的依赖项项目聚合到一个 dll 中,并仅为这个结果 dll 制作 SQL 脚本.dacpac文件。

I'm using ILMerge.MSBuild.Tasks.ILMerge to aggregate all dll on AfterBuild event in sqlproj.我正在使用 ILMerge.MSBuild.Tasks.ILMerge 在 sqlproj 中的 AfterBuild 事件上聚合所有 dll。 But this aggregation happens after generating SQL script.但是这种聚合发生在生成 SQL 脚本之后。

How can I enforce SQL script generation in the end?最后如何强制生成 SQL 脚本?

Build log:构建日志:

2>------ Rebuild All started: Project: TestCLR, Configuration: Debug Any CPU ------
2>      D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /nowarn:1701,1702,2008 /fullpaths /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\mscorlib.dll" /reference:D:\Work\FF\Sql-Objects\Tools\TestCLR\bin\Debug\TestLogic.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Xml.dll" /debug+ /debug:full /optimize- /out:obj\Debug\TestCLR.dll /subsystemversion:6.00 /target:library /warnaserror- /utf8output /langversion:7.3 TestProcedures.cs 
2>      Loading project references...
2>      Loading project files...
2>      Building the project model and resolving object interdependencies...
2>      Validating the project model...
2>      Writing model to D:\Work\FF\Sql-Objects\Tools\TestCLR\obj\Debug\Model.xml...
2>      Writing create script to TestCLR_Create.sql...
2>      TestCLR -> D:\Work\FF\Sql-Objects\Tools\TestCLR\bin\Debug\TestCLR.dll
2>      TestCLR -> D:\Work\FF\Sql-Objects\Tools\TestCLR\bin\Debug\TestCLR.dacpac
2>      ILMerge bin\Debug\TestCLR.dll;bin\Debug\TestLogic.dll;bin\Debug\Sider.dll -> D:\Work\FF\Sql-Objects\Tools\TestCLR\bin\Debug\TestCLR.dll
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========

.sqlproj part with ILMerge (Right now it's on the last place. Right before /Project tag ) .sqlproj 部分与 ILMerge(现在它在最后一个位置。就在 /Project 标签之前)

  <UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
  <Target Name="AfterBuild">
    <ItemGroup>
      <MergeAsm Include="bin\Debug\*.dll" />
    </ItemGroup>
    <PropertyGroup>
      <MergedAssembly>$(ProjectDir)$(OutDir)RedisCLR.dll</MergedAssembly>
    </PropertyGroup>
    <Message Text="ILMerge @(MergeAsm) -&gt; $(MergedAssembly)" Importance="high" />
    <ILMerge InputAssemblies="@(MergeAsm)" OutputFile="$(MergedAssembly)" TargetKind="SameAsPrimaryAssembly" />
  </Target>

Much appreciate Mr. Solomon Rutzky.非常感谢所罗门·鲁茨基先生。 This solution was wery helpful.这个解决方案很有帮助。

<PropertyGroup>
  <SqlBuildDependsOn>
    BeforeSqlBuild;
    $(SqlBuildDependsOn);
  </SqlBuildDependsOn>
</PropertyGroup>
<Target Name="BeforeSqlBuild">
  ILMerge things
</Target>

But there was one curious thing.但是有一件奇怪的事情。 Dll comes to Bin\\debug in the end of flow (I think on PostBuildEvent step or near). Dll 在流程结束时来到 Bin\\debug (我认为在 PostBuildEvent 步骤或附近)。 And if I move ILMerge upper on the flow - it returns error, that there are no DLLs.如果我将 ILMerge 移到流程的上层 - 它会返回错误,即没有 DLL。 Changed config to gater dll from obj folder and from dependant project.将 obj 文件夹和依赖项目中的配置更改为 gater dll。

Interesting question.有趣的问题。 I don't know of any easy way to do this, or if it's even possible in the first place.我不知道有什么简单的方法可以做到这一点,或者一开始是否有可能。 However, there are two options that I know of for altering the build process:但是,我知道有两个选项可以改变构建过程:

Option 1选项1

Inject a build step / target.注入构建步骤/目标。 SSDT is missing at least two build steps/targets — "BeforeSqlBuild" and "BeforePublish" — because the "AfterBuild" target is too late in the process (as you have discovered). SSDT 至少缺少两个构建步骤/目标——“BeforeSqlBuild”和“BeforePublish”——因为“AfterBuild”目标在这个过程中为时已晚(如您所见)。

To get around the "CLR strict security" debacle introduced in SQL Server 2017 (documented in the following post of mine: SQLCLR vs. SQL Server 2017, Part 3: “CLR strict security” – Solution 2 ), I updated the .sqlproj file by placing the following at the end, just before the closing </Project> tag (to sign the assembly with a certificate):为了解决 SQL Server 2017 中引入的“CLR 严格安全性”问题(记录在我的以下帖子中: SQLCLR 与 SQL Server 2017,第 3 部分:“CLR 严格安全性”——解决方案 2 ),我更新了.sqlproj文件通过将以下内容放在最后,就在结束</Project>标记之前(用证书签署程序集):

<PropertyGroup>
  <SqlBuildDependsOn>
    BeforeSqlBuild;
    $(SqlBuildDependsOn);
  </SqlBuildDependsOn>
</PropertyGroup>
<Target Name="BeforeSqlBuild">
  <Exec Command="{DOS / Windows commands}"/>
</Target>

You can keep the <Exec Command="..."/> and/or add the contents of your existing "AfterBuild" target.您可以保留<Exec Command="..."/>和/或添加现有“AfterBuild”目标的内容。 HOWEVER, even with this performing the action at the correct time, I'm not entirely certain that you will be able to change the assembly name that it will want to serialize and put in the "Create" script (ie switch from using "TestCLR.dll" to "RedisCLR.dll").但是,即使在正确的时间执行操作,我也不能完全确定您是否能够更改它想要序列化并放入“创建”脚本的程序集名称(即从使用“TestCLR .dll”到“RedisCLR.dll”)。 You can try re-assigning the build variable, but I've never tried that and am not sure it's allowed.您可以尝试重新分配构建变量,但我从未尝试过并且不确定是否允许。 In which case the next option might help.在这种情况下,下一个选项可能会有所帮助。

(Please vote for my enhancement request to improve the SSDT build process: (请投票支持我的改进请求以改进 SSDT 构建过程:
Add MSBuild predefined Targets for "BeforeSqlBuild" and "BeforePublish" to SSDT SQL Server Database Projects ) 将“BeforeSqlBuild”和“BeforePublish”的 MSBuild 预定义目标添加到 SSDT SQL Server 数据库项目

Option 2选项 2

Use .NET to create a custom build task that has access to the objects, files, and overall process:使用 .NET 创建可以访问对象、文件和整个过程的自定义构建任务:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM