Build a database project from command line

While the SQL Database Project extension provides a graphical user interface to build a database project, a command line build experience is also available for Windows, macOS, and Linux environments. The steps to build a project from the command line are different between SDK-style SQL projects and the previous non-SDK-style SQL project format. This article outlines the prerequisites and syntax needed to build a dacpac from a SQL project at the command line for both SQL project types.

SDK-style SQL projects (preview)

Using Microsoft.Build.Sql with SDK-style SQL projects is the preferred method for working with SQL projects from the command line.

To build an SDK-style SQL project from the command line on Windows, macOS, or Linux, use the following command:

dotnet build /p:NetCoreBuild=true

Note

The SDK-style SQL projects is currently in preview.

Non-SDK-style SQL projects

The following section describes a work-around for working with non-SDK-style SQL projects from the command line on Windows, macOS, and Linux.

Prerequisites

  1. Install and configure SQL Database Projects extension.

  2. The following .NET Core dlls and the target file Microsoft.Data.Tools.Schema.SqlTasks.targets are required to build a SQL database project from the command line from all platforms supported by the Azure Data Studio extension for SQL Database Projects. These files are created by the extension during the first build completed in the Azure Data Studio interface and placed in the extension's folder under BuildDirectory. For example, on Linux, these files are placed in ~\.azuredatastudio\extensions\microsoft.sql-database-projects-x.x.x\BuildDirectory\. Copy these 11 files to a new and accessible folder or note their location. This location is referred to as DotNet Core build folder in this document.

    • Microsoft.Data.SqlClient.dll
    • Microsoft.Data.Tools.Schema.Sql.dll
    • Microsoft.Data.Tools.Schema.SqlTasks.targets
    • Microsoft.Data.Tools.Schema.Tasks.Sql.dll
    • Microsoft.Data.Tools.Utilities.dll
    • Microsoft.SqlServer.Dac.dll
    • Microsoft.SqlServer.Dac.Extensions.dll
    • Microsoft.SqlServer.TransactSql.ScriptDom.dll
    • Microsoft.SqlServer.Types.dll
    • System.ComponentModel.Composition.dll
    • System.IO.Packaging.dll
  3. If the project was created in Azure Data Studio - skip ahead to Build the project from the command line. If the project was created in SQL Server Data Tools (SSDT), open the project in the Azure Data Studio SQL Database project extension. Opening the project in Azure Data Studio automatically updates the sqlproj file with three edits, noted for your information:

    1. Import conditions
    <Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(NETCoreTargetsPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets"/> 
    <Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets"/>
    <Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets"/>
    
    1. Package reference
    <ItemGroup>
        <PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All"/>
    </ItemGroup>
    
    1. Clean target, necessary for supporting dual editing in SQL Server Data Tools (SSDT) and Azure Data Studio
    <Target Name="AfterClean">
        <Delete Files="$(BaseIntermediateOutputPath)\project.assets.json"/>
    </Target>
    

Build the project from the command line

From the full .NET folder, use the following command:

dotnet build "<sqlproj file path>" /p:NetCoreBuild=true /p:NETCoreTargetsPath="<DotNet Core build folder>"

For example, from /usr/share/dotnet on Linux:

dotnet build "/home/myuser/Documents/DatabaseProject1/DatabaseProject1.sqlproj" /p:NetCoreBuild=true /p:NETCoreTargetsPath="/home/myuser/.azuredatastudio/extensions/microsoft.sql-database-projects-x.x.x/BuildDirectory"  

Next steps