Udostępnij przez


Szybki start: generowanie kodu

Z tego Szybkiego startu dowiesz się, jak GitHub Copilot przyspiesza tworzenie SQL i mapowania obiektowo-relacyjnego (ORM), generując kod świadomy kontekstu bezpośrednio w Visual Studio Code. GitHub Copilot ułatwia tworzenie szkieletów tabel, rozwijanie schematów i zmniejszanie powtarzalnych skryptów. Niezależnie od tego, czy używasz języka T-SQL, czy pracujesz z maszynami ORM, takimi jak Entity Framework, Sequelize, Prisma lub SQLAlchemy, GitHub Copilot pomaga skupić się na tworzeniu logiki aplikacji.

Wprowadzenie

Upewnij się, że masz połączenie z bazą danych i masz otwarte aktywne okno edytora z rozszerzeniem MSSQL. To połączenie umożliwia uczestnikowi czatu @mssql zrozumienie kontekstu środowiska bazy danych, umożliwiając dokładne i kontekstowe sugestie. Bez połączenia z bazą danych uczestnik czatu nie będzie miał schematu ani kontekstu danych w celu zapewnienia znaczących odpowiedzi.

W poniższych przykładach użyto przykładowej bazy danych AdventureWorksLT2022, którą można pobrać ze strony głównej Przykładów i projektów społeczności programu Microsoft SQL Server.

Aby uzyskać najlepsze wyniki, dostosuj nazwy tabel i schematów, aby dopasować je do własnego środowiska.

Upewnij się, że czat zawiera @mssql prefiks. Na przykład wpisz @mssql swoje pytanie lub polecenie. Dzięki temu uczestnik czatu rozumie, że prosi o pomoc związaną z językiem SQL.

Generowanie kodu za pomocą narzędzia GitHub Copilot

Użyj narzędzia GitHub Copilot, aby wygenerować kod zgodny z językiem SQL i ORM, który odzwierciedla strukturę połączonej bazy danych i postępuje zgodnie z najlepszymi rozwiązaniami. Od definiowania tabel i relacji po tworzenie skryptów widoków, budowanie plików migracyjnych czy szkieletowanie warstw dostępu do danych i interfejsów API, GitHub Copilot pomaga przyspieszyć pracę i zwiększyć pewność siebie.

Oto typowe przypadki użycia i przykłady tego, co można zadać za pośrednictwem uczestnika czatu:

Generowanie kodu SQL

Narzędzie GitHub Copilot może ułatwić generowanie kodu SQL dla kilku scenariuszy programowania, od tworzenia skryptów, tworzenia i modyfikowania tabel do pisania procedur składowanych i widoków. W tych przykładach pokazano, jak za pomocą narzędzia GitHub Copilot zautomatyzować powtarzające się skrypty SQL i postępować zgodnie z najlepszymi rozwiązaniami dotyczącymi programowania w języku T-SQL.

Wygeneruj skrypty dla wszystkich tabel w schemacie

Script out all the tables in the `SalesLT` schema as `CREATE TABLE` statements in SQL.

Pobieranie danych klienta za pomocą procedury składowanej

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.

Wygenerować skrypt tabeli ze wszystkimi ograniczeniami i indeksami

Script out the `SalesLT.Customer` table as a `CREATE TABLE` statement, including all constraints and indexes.

Napisać skrypt widoku, który łączy dwie tabele

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.

Modyfikowanie tabeli przez dodanie nowej kolumny

Write a SQL script to alter the `SalesLT.Customer` table by adding a `last_updated` column with a default timestamp.

Generowanie migracji ORM

Narzędzie GitHub Copilot może generować migracje zgodne z orm i definicje modeli na podstawie kontekstu schematu i wybranej struktury. Od Sequelize do Entity Framework, Prisma i SQLAlchemy, GitHub Copilot pomaga w tworzeniu szkieletowych zmian, które są zgodne z modelem danych aplikacji.

Generowanie modelu w celu dodania kolumny

Generate a Sequelize (JavaScript) model to add an `email` column (`varchar(256)`) to the `SalesLT.Customer` table.

Generowanie klasy modelu programu Entity Framework

Generate an Entity Framework model class in C# to represent a `SalesLT.ProductModel` table with `id`, `name`, and `description` columns.

Generowanie modelu platformy Entity Framework

Generate an Entity Framework model in C# based on the existing `SalesLT.Product` table.

Pisanie kodu w języku Python w celu zdefiniowania tabeli

Write SQLAlchemy code to define a `SalesLT.OrderDetails` table with `id`, `order_date`, and `customer_id` fields, ensuring compatibility with `Python`.

Pisanie zapytania sparametryzowanego

Using SQLAlchemy, write a parameterized query that retrieves all customers from the `SalesLT.Customer` table where the `LastName` matches a provided parameter.

Aktualizowanie istniejącego modelu w celu dodania tabeli

Update my existing Prisma model (schema.prisma) to define a new `SalesLT.Order` model with `id`, `customer_id`, and `order_date` fields.

Generowanie klasy modelu dla tabeli

Generate a SQLAlchemy model class for the `SalesLT.Product` table, including columns and data types.

Generowanie szablonowego kodu aplikacji

Narzędzie GitHub Copilot może również pomóc w utworzeniu szkieletu komponentów backendu i frontendu, które współdziałają z bazą danych SQL. W tych przykładach pokazano, jak można przejść ze schematu do działającego kodu aplikacji przy użyciu popularnych stosów, takich jak Azure Functions, Node.js, Django i Next.js.

Bezserwerowe powiązania SQL dla komponentów zaplecza i Blazor

W poniższym przykładzie przedstawiono pełne podpowiedzi, których można użyć z GitHub Copilot Chat, aby opracować kompleksowe rozwiązanie. Podpowiedzi te zawierają szczegółowe instrukcje i kontekst, aby ułatwić Copilotowi generowanie dokładnego, ustrukturyzowanego kodu zarówno w warstwach backendu, jak i frontendu.

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.

Pełny stos technologiczny z technologiami Node.js i Next.js

Poniższy przykład to szczegółowy monit, który można podać w aplikacji GitHub Copilot Chat w celu wygenerowania pełnej konfiguracji zaplecza, w tym tras interfejsu API i integracji bazy danych.

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.

Zaplecze: Django + Struktura REST Django

Poniższy przykład to szczegółowy monit, który można podać w aplikacji GitHub Copilot Chat w celu wygenerowania pełnej konfiguracji zaplecza, w tym tras interfejsu API i integracji bazy danych.

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`.

Podziel się swoimi doświadczeniami

Aby pomóc nam udoskonalić i ulepszyć narzędzie GitHub Copilot dla rozszerzenia MSSQL, użyj następującego szablonu problemu GitHub, aby przesłać swoją opinię: GitHub Copilot Feedback

Podczas przesyłania opinii należy wziąć pod uwagę następujące kwestie:

  • Przetestowane scenariusze — daj nam znać obszary, na przykład tworzenie schematu, generowanie zapytań, zabezpieczenia, lokalizację.

  • Co się udało — opisz wszelkie doświadczenia, które przebiegły sprawnie, były pomocne lub przerosły twoje oczekiwania.

  • Problemy lub błędy — obejmują wszelkie problemy, niespójności lub mylące zachowania. Zrzuty ekranu lub nagrania ekranu są szczególnie przydatne.

  • Sugestie dotyczące poprawy — podziel się pomysłami na poprawę użyteczności, rozszerzaniem zasięgu lub ulepszaniem odpowiedzi w usłudze GitHub Copilot.