有没有办法在不发布的情况下推导出 dacpac 的版本?

Is there a way to derive the version of a dacpac without publishing it?

正如标题所述,我需要能够在将 dacpac 发布到数据库之前以编程方式验证它的版本。有办法吗?

您可以使用 AssemblyInfo 执行此操作:

  1. 如果它不存在;创建Properties\AssemblyInfo.cs。确保它包含在项目中并且至少包含以下代码:
using System.Reflection; 
[assembly: AssemblyVersion("1.0.*")] // Define however you want your version to be
  1. 在外部编辑器(记事本等)中编辑 *.sqlproj 文件并添加以下内容 XML:
    <Target Name="SetDacVersionToAssemblyVersion" AfterTargets="CoreCompile">
        <GetAssemblyIdentity AssemblyFiles="$(IntermediateTargetFullFileName)">
            <Output TaskParameter="Assemblies" PropertyName="IntermediateTargetAssembly" />
        </GetAssemblyIdentity>
        <PropertyGroup>
            <DacVersion>$(IntermediateTargetAssembly.Split(',')[1].Split('=')[1])
            </DacVersion>
        </PropertyGroup>
        <Message Text="DacVersion set to $(DacVersion)" Importance="high" />
    </Target>
  1. 使用 PowerShell 获取 AssemblyVersion
Get-ChildItem -Filter *.dacpac -Recurse | Select-Object Name,@{n='FileVersion';e={$_.VersionInfo.FileVersion}},@{n='AssemblyVersion';e={[Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version}}