共用方式為


快速入門:搭配 SQL 使用數據 API 產生器

在本快速入門中,您會建置一組以本機 SQL 資料庫為目標的數據 API 產生器組態檔。

必要條件

提示

或者,在 GitHub Codespaces 中開啟本快速入門,並已安裝所有開發人員必要條件。 只要自備 Azure 訂用帳戶即可。 GitHub 帳戶包含記憶體的權利和核心時數,不收費。 如需詳細資訊,請參閱 GitHub 帳戶的包含記憶體和核心時數

在 GitHub Codespaces 中開啟

安裝資料 API 產生器 CLI

將 NuGet 中的 Microsoft.DataApiBuilder 套件安裝為 .NET 工具。

  1. 使用 dotnet tool install 安裝具有 自變數的Microsoft.DataApiBuilder--global最新版本。

    dotnet tool install --global Microsoft.DataApiBuilder
    

    注意

    如果已安裝套件,您將改用 dotnet tool update來更新套件。

    dotnet tool update --global Microsoft.DataApiBuilder
    
  2. 使用 自變數確認工具已安裝dotnet tool list--global

    dotnet tool list --global
    

設定本機資料庫

首先,設定並執行本機資料庫來設定相關的認證。 然後,您可以使用範例數據植入資料庫。

  1. 從 Docker Hub 取得容器映像的最新複本mcr.microsoft.com/mssql/server:2022-latest

    docker pull mcr.microsoft.com/mssql/server:2022-latest
    
  2. 藉由設定密碼、接受使用者許可協定 (EULA) ,以及發佈埠 1433 來啟動 Docker 容器。 以自訂密碼取代 <your-password>

    docker run \
        --env "ACCEPT_EULA=Y" \
        --env "MSSQL_SA_PASSWORD=<your-password>" \
        --publish 1433:1433 \
        --detach \
        mcr.microsoft.com/mssql/server:2022-latest
    
  3. 使用您慣用的數據管理環境連線到您的本機資料庫。 範例包括,但不限於:SQL Server Management StudioAzure Data Studio,以及適用於 Visual Studio Code 的 SQL Server 延伸模組

    提示

    如果您使用 Docker Linux 容器映像的預設網路功能,連接字串 可能是 Server=localhost,1433;User Id=sa;Password=<your-password>;TrustServerCertificate=True;Encrypt=True;。 將取代 <your-password> 為您稍早設定的密碼。

  4. Create 新的bookshelf資料庫,並將資料庫用於其餘查詢。

    DROP DATABASE IF EXISTS bookshelf;
    GO
    
    CREATE DATABASE bookshelf;
    GO
    
    USE bookshelf;
    GO
    
  5. Create 新的dbo.authors數據表,並使用基本數據植入數據表。

    DROP TABLE IF EXISTS dbo.authors;
    GO
    
    CREATE TABLE dbo.authors
    (
        id int not null primary key,
        first_name nvarchar(100) not null,
        middle_name nvarchar(100) null,
        last_name nvarchar(100) not null
    )
    GO
    
    INSERT INTO dbo.authors VALUES
        (01, 'Henry', null, 'Ross'),
        (02, 'Jacob', 'A.', 'Hancock'),
        (03, 'Sydney', null, 'Mattos'),
        (04, 'Jordan', null, 'Mitchell'),
        (05, 'Victoria', null, 'Burke'),
        (06, 'Vance', null, 'DeLeon'),
        (07, 'Reed', null, 'Flores'),
        (08, 'Felix', null, 'Henderson'),
        (09, 'Avery', null, 'Howard'),
        (10, 'Violet', null, 'Martinez')
    GO
    

建立組態檔

使用 DAB CLI Create 基準組態檔。 然後,使用您目前的認證新增開發組態檔。

  1. 使用 Create 一般組態檔dab init--connection-string從第一個區段新增自變數與資料庫 連接字串。 將取代 <your-password> 為您稍早在本指南中設定的密碼。 此外,將Database=bookshelf值新增至 連接字串。

    dab init --database-type "mssql" --host-mode "Development" --connection-string "Server=localhost,1433;User Id=sa;Database=bookshelf;Password=<your-password>;TrustServerCertificate=True;Encrypt=True;"
    
  2. 使用 dab add新增 Author 實體。

    dab add Author --source "dbo.authors" --permissions "anonymous:*"
    

使用本機資料庫測試 API

現在,啟動數據 API 產生器工具,以驗證您的組態檔在開發期間是否合併。

  1. 使用 dab start 來執行工具,併為您的實體建立 API 端點。

    dab start
    
  2. 工具的輸出應該包含用來巡覽至執行中 API 的位址。

          Successfully completed runtime initialization.
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: <http://localhost:5000>
    info: Microsoft.Hosting.Lifetime[0]
    

    提示

    在此範例中,應用程式正在埠 5000localhost執行。 執行中的應用程式可能有不同的位址和埠。

  3. 首先,向 發出 GET 要求以 /api/Author手動方式嘗試 API。

    提示

    在這裡範例中,URL 會是 https://localhost:5000/api/Author。 您可以使用網頁瀏覽器流覽至此 URL。

  4. 接下來,流覽至 位於 /swagger的 Swagger 檔頁面。

    提示

    在這裡範例中,URL 會是 https://localhost:5000/swagger。 同樣地,您可以使用網頁瀏覽器流覽至此 URL。

後續步驟