從命令列建置資料庫專案

雖然 SQL 資料庫 Project 延伸模組提供圖形化使用者介面來 建置資料庫專案 ,但命令列建置體驗也適用于 Windows、macOS 和 Linux 環境。 SDK 式 SQL 專案與過去非 SDK 式 SQL 專案格式,兩者從命令列建置專案的步驟不一樣。 本文概述在這兩種 SQL 專案類型命令列上,從 SQL 專案建置 dacpac 所需的必要條件和語法。

SDK 樣式的 SQL 專案 (預覽)

SDK 式 SQL 專案使用 Microsoft.Build.Sql,是從命令列處理 SQL 專案的慣用方法。

若要在 Windows、macOS 或 Linux 中使用命令列建置 SDK 式 SQL 專案,請使用下列命令:

dotnet build /p:NetCoreBuild=true

注意

SDK 式 SQL 專案目前為預覽狀態。

非 SDK 式 SQL 專案

下一節說明如何從 Windows、macOS 和 Linux 命令列使用非 SDK 樣式 SQL 專案。

必要條件

  1. 安裝並設定 SQL Database Projects 延伸模組

  2. 需要有下列 .NET Core dll 與目標檔案 Microsoft.Data.Tools.Schema.SqlTasks.targets,才能在適用於 SQL 資料庫專案之 Azure Data Studio 延伸模組所支援的所有平台中,從命令列建置 SQL 資料庫專案。 這些檔案是在 Azure Data Studio 介面中完成的第一個組建期間由延伸模組所建立,並放置於 BuildDirectory 之下延伸模組的資料夾中。 例如,在 Linux 上,這些檔案會放置於 ~\.azuredatastudio\extensions\microsoft.sql-database-projects-x.x.x\BuildDirectory\ 中。 將這 11 個檔案複製到可存取的新資料夾,或記下其位置。 在此文件中,此位置將稱為 DotNet Core build folder

    • 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. 如果專案在 Azure Data Studio 中建立的,請直接跳到從命令列建置專案。 如果專案是在 SQL Server Data Tools (SSDT) 中建立的,則在 Azure Data Studio SQL 資料庫專案延伸模組中開啟專案。 在 Azure Data Studio 中開啟專案,即會自動更新包含三個編輯的 sqlproj 檔案,如下所示:

    1. 匯入條件
    <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. 套件參考
    <ItemGroup>
        <PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All"/>
    </ItemGroup>
    
    1. 清除在 SQL Server Data Tools (SSDT) 與 Azure Data Studio 中支援雙重編輯所需的目標
    <Target Name="AfterClean">
        <Delete Files="$(BaseIntermediateOutputPath)\project.assets.json"/>
    </Target>
    

從命令列建置專案

從完整的 .NET 資料夾中,使用下列命令:

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

例如,從 Linux 上的 /usr/share/dotnet

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"  

後續步驟