共用方式為


快速入門:使用 SQL 與資料 API 建立工具

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

先決條件

小提示

或者,直接在 GitHub Codespaces 中開啟本快速入門,所有開發所需的先決條件已經預先安裝完成。 只需自備 Azure 訂用帳戶即可。 GitHub 帳戶的免費權利包括儲存體和核心時數。 如需詳細資訊,請參閱 GitHub 帳戶包含的儲存體和核心時數

在 GitHub Codespaces 中開啟

安裝資料 API 產生器 CLI

在 NuGet 安裝Microsoft.DataApiBuilder套件作為 .NET 工具。

  1. 使用 dotnet tool install--global 參數安裝最新版本的 Microsoft.DataApiBuilder

    dotnet tool install --global Microsoft.DataApiBuilder
    

    備註

    如果已安裝套件,請使用 dotnet tool update來更新套件。

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

    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. 建立新的 bookshelf 資料庫,並使用資料庫進行剩餘的查詢。

    DROP DATABASE IF EXISTS bookshelf;
    GO
    
    CREATE DATABASE bookshelf;
    GO
    
    USE bookshelf;
    GO
    
  5. 建立新的 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 建立基準組態檔。 然後,使用您目前的認證來新增開發組態檔。

  1. 使用 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

現在,啟動 Data 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。

後續步驟