このクイック スタートでは、GitHub Copilot が Visual Studio Code 内で直接コンテキスト対応コードを生成することで、SQL とオブジェクト リレーショナル マッピング (ORM) の開発を高速化する方法について説明します。 GitHub Copilot は、テーブルのスキャフォールディング、スキーマの進化、反復的なスクリプトの削減に役立ちます。 T-SQL を使用している場合でも、Entity Framework、続編、Prisma、SQLAlchemy などの ORM を使用する場合でも、GitHub Copilot はアプリケーション ロジックの構築に集中するのに役立ちます。
概要
データベースに接続されていて、MSSQL 拡張機能でアクティブなエディター ウィンドウが開かれていることを確認します。 この接続により、 @mssql チャット参加者はデータベース環境のコンテキストを理解できるため、正確でコンテキストに対応した提案が可能になります。 データベース接続がないと、チャット参加者は意味のある応答を提供するスキーマまたはデータ コンテキストを持ちません。
次の例では、 AdventureWorksLT2022 サンプル データベースを使用します。このデータベースは、 Microsoft SQL Server サンプルとコミュニティ プロジェクト のホーム ページからダウンロードできます。
最適な結果を得るには、独自の環境に合わせてテーブル名とスキーマ名を調整します。
チャットに @mssql プレフィックスが含まれていることを確認します。 たとえば、「 @mssql 」と入力し、その後に質問またはプロンプトを入力します。 これにより、チャット参加者は、SQL 関連のサポートを求めていることを理解できます。
GitHub Copilot を使用したコード生成
GitHub Copilot を使用して、接続されたデータベースの構造を反映し、ベスト プラクティスに従う SQL と ORM 互換のコードを生成します。 テーブルとリレーションシップの定義から、スクリプト ビュー、移行ファイルの構築、データ アクセスレイヤーと API のスキャフォールディングまで、GitHub Copilot は、迅速かつ確実に移動するのに役立ちます。
チャット参加者から質問できる一般的なユース ケースと例を次に示します。
SQL コードを生成する
GitHub Copilot は、スクリプト作成、テーブルの作成、変更からストアド プロシージャとビューの記述まで、いくつかの開発シナリオに対応した SQL コードを生成するのに役立ちます。 これらの例は、GitHub Copilot を使用して反復的な SQL スクリプトを自動化し、T-SQL 開発のベスト プラクティスに従う方法を示しています。
スキーマ内のすべてのテーブルのスクリプトを作成する
Script out all the tables in the `SalesLT` schema as `CREATE TABLE` statements in SQL.
ストアド プロシージャを使用して顧客データを取得する
Write a SQL stored procedure in my current database. The procedure should retrieve all customers from the `SalesLT.Customer` table where the `LastName` matches a given parameter. Make sure to use T-SQL best practices.
すべての制約とインデックスを使用してテーブルをスクリプト化する
Script out the `SalesLT.Customer` table as a `CREATE TABLE` statement, including all constraints and indexes.
2 つのテーブルを結合するビューのスクリプトを作成する
Generate a SQL script to create a view that joins the `SalesLT.Customer` and `SalesLT.SalesOrderHeader` tables, showing customer names and their total order amounts.
新しい列を追加してテーブルを変更する
Write a SQL script to alter the `SalesLT.Customer` table by adding a `last_updated` column with a default timestamp.
ORM マイグレーションを生成
GitHub Copilot では、選択したスキーマ コンテキストとフレームワークに基づいて、ORM 互換の移行とモデル定義を生成できます。 SequelizeからEntity Framework、Prisma、SQLAlchemyまで、GitHub Copilotは、アプリケーションのデータモデルに合った変更の足場を作るのに支援します。
列を追加するモデルを生成する
Generate a Sequelize (JavaScript) model to add an `email` column (`varchar(256)`) to the `SalesLT.Customer` table.
Entity Framework モデル クラスの生成
Generate an Entity Framework model class in C# to represent a `SalesLT.ProductModel` table with `id`, `name`, and `description` columns.
Entity Framework モデルの生成
Generate an Entity Framework model in C# based on the existing `SalesLT.Product` table.
テーブルを定義する Python コードを記述する
Write SQLAlchemy code to define a `SalesLT.OrderDetails` table with `id`, `order_date`, and `customer_id` fields, ensuring compatibility with `Python`.
パラメーター化されたクエリを記述する
Using SQLAlchemy, write a parameterized query that retrieves all customers from the `SalesLT.Customer` table where the `LastName` matches a provided parameter.
既存のモデルを更新してテーブルを追加する
Update my existing Prisma model (schema.prisma) to define a new `SalesLT.Order` model with `id`, `customer_id`, and `order_date` fields.
テーブルのモデル クラスを生成する
Generate a SQLAlchemy model class for the `SalesLT.Product` table, including columns and data types.
定型アプリ コードを生成する
GitHub Copilot は、SQL データベースと対話するバックエンドコンポーネントとフロントエンド コンポーネントのスキャフォールディングにも役立ちます。 これらの例では、Azure Functions、Node.js、Django、Next.jsなどの一般的なスタックを使用して、スキーマから動作するアプリケーション コードに進む方法を示します。
サーバーレス バックエンドのSQL接続とBlazor
次の例は、GitHub Copilot Chat でエンド ツー エンド ソリューションをスキャフォールディングするために使用できる完全なプロンプトを示しています。 これらのプロンプトには、Copilot がバックエンドレイヤーとフロントエンドレイヤーの両方で正確で構造化されたコードを生成するのに役立つ詳細な手順とコンテキストが含まれています。
Generate a full-stack app using Azure SQL bindings for Functions and Blazor WebAssembly. Follow these steps:
1. Backend: Azure Functions (C#) with SQL Bindings
- Configure SQL Bindings to automatically read and write data from the `SalesLT.Customer` table.
- Implement HTTP-triggered functions with the following endpoints:
- `GET /api/customers` – Fetch all customers.
- `GET /api/customers/{id}` – Get a specific customer by ID.
- `POST /api/customers` – Create a new customer.
- `PUT /api/customers/{id}` – Update an existing customer.
- `DELETE /api/customers/{id}` – Delete a customer.
- Use `Dependency Injection` for database connections and logging.
- Include an `appsettings.json` file to store database connection strings and environment variables.
- Use `Azure Functions Core Tools` to run and test the functions locally.
1. Frontend: Blazor WebAssembly (Optional)
- Create a Blazor WebAssembly frontend that consumes the API.
- Display a table with customer data and a form to add new customers.
- Use `HttpClient` to call the Azure Functions endpoints.
- Implement two-way data binding to handle form inputs dynamically.
- Use Bootstrap or Blazor components for styling and layout.
Ensure the project includes setup instructions for running both the Azure Functions backend and Blazor WebAssembly frontend locally, with proper `.env` or `local.settings.json` configurations for database connections.
Node.js と Next.js を使用したフル スタック
次の例は、API ルートやデータベース統合など、バックエンドの完全なセットアップを生成するために GitHub Copilot Chat で指定できる詳細なプロンプトです。
Generate a REST API using Node.js with Express that connects to my local SQL Database. Use the Tedious package for SQL Server connections and Prisma as the ORM. Follow these steps:
1. Backend: Node.js + Express
- Establish a database connection using Prisma with Tedious as the SQL Server driver.
- Implement API routes for `SalesLT.Customer` with the following endpoints:
- `GET /customers` – Fetch all customers.
- `GET /customers/:id` – Get a specific customer by ID.
- `POST /customers` – Create a new customer.
- `PUT /customers/:id` – Update an existing customer.
- `DELETE /customers/:id` – Delete a customer.
- Configure `Prisma` to map the `SalesLT.Customer` table and generate database migrations using `prisma migrate dev`.
- Use `dotenv` for environment variables (database credentials, ports, etc.).
- Add `Jest` for testing the API endpoints.
1. Frontend: Next.js + TypeScript (Optional)
- Create a Next.js frontend that consumes the API.
- Display a table with customer data and a form to add new customers.
- Use React hooks (`useState`, `useEffect`) to manage state and fetch data dynamically.
- Style the UI using Tailwind CSS.
- Implement server-side data fetching (`getServerSideProps`) in Next.js for improved performance.
Ensure the project includes setup instructions for running both the backend and frontend independently, with proper `.env` configurations for the database connection.
バックエンド: Django + Django REST フレームワーク
次の例は、API ルートやデータベース統合など、バックエンドの完全なセットアップを生成するために GitHub Copilot Chat で指定できる詳細なプロンプトです。
Scaffold a Django backend with Django REST Framework for the `SalesLT.Customer` table. Follow these steps:
- Implement API routes using Django's `ModelViewSet` with the following endpoints:
- `GET /customers` – Fetch all customers.
- `GET /customers/{id}` – Get a specific customer by ID.
- `POST /customers` – Create a new customer.
- `PUT /customers/{id}` – Update an existing customer.
- `DELETE /customers/{id}` – Delete a customer.
- Add instructions for generating database migrations with `python manage.py makemigrations` and `migrate`.
感想をお聞かせください
MSSQL 拡張機能の GitHub Copilot を改良および改善するために、次の GitHub 問題テンプレートを使用してフィードバックを送信します。 GitHub Copilot フィードバック
フィードバックを送信する場合は、次の内容を検討してください。
テスト済みのシナリオ – スキーマの作成、クエリの生成、セキュリティ、ローカライズなど、どの領域に重点を置いたかをお知らせください。
うまくいったこと - スムーズで役に立ち、期待を超えた経験について説明します。
問題またはバグ – 問題、不整合、または混乱を招く動作を含めます。 スクリーンショットや画面の記録は特に役立ちます。
改善の提案 – 使いやすさの向上、カバレッジの拡大、GitHub Copilot の応答の強化に関するアイデアを共有します。
関連コンテンツ
- Visual Studio Code 用 MSSQL 拡張機能のための GitHub Copilot
- クイックスタート: チャット機能とGitHub Copilotのインライン提案を使う
- クイック スタート: スキーマ エクスプローラーとデザイナーを使用する
- クイック スタート: スマート クエリ ビルダーを使用する
- クイック スタート: クエリ オプティマイザー アシスタント
- クイックスタート: ビジネスロジック解説機能の使い方
- クイック スタート: セキュリティ アナライザー
- クイック スタート: ローカライズと書式設定ヘルパー
- クイックスタート: テストとモックテスト作成のためにデータを生成する
- 制限事項と既知の問題