Search code examples
sql-serversql-server-data-tools

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


Just as the title states, I need to be able to programatically validate the version of a dacpac prior to publishing it to a database. Is there a way to do this?


Solution

  • You can do this with AssemblyInfo:

    1. if it doesn't already exist; Create Properties\AssemblyInfo.cs. Make sure it's included in the project and contains at least this code:
    using System.Reflection; 
    [assembly: AssemblyVersion("1.0.*")] // Define however you want your version to be
    
    1. Edit the *.sqlproj file in an outside editor (notepad etc) and add the following 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. Get the AssemblyVersion with PowerShell
    Get-ChildItem -Filter *.dacpac -Recurse | Select-Object Name,@{n='FileVersion';e={$_.VersionInfo.FileVersion}},@{n='AssemblyVersion';e={[Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version}}