快速入门:生成代码

在本快速入门中,你将了解如何通过直接在 Visual Studio Code 中生成上下文感知代码来加速 SQL 和对象关系映射 (ORM) 开发。 GitHub Copilot 可帮助你搭建表、改进架构并减少重复的脚本编写。 无论是使用 T-SQL 还是使用实体框架、Sequelize、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.

编写连接两个表的视图脚本

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.

生成实体框架模型

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 的全堆栈

以下示例是 GitHub Copilot Chat 中提供的详细提示,用于生成完整的后端设置,包括 API 路由和数据库集成。

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 框架

以下示例是 GitHub Copilot Chat 中提供的详细提示,用于生成完整的后端设置,包括 API 路由和数据库集成。

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 响应的想法。