Introduction
GitHub Copilot is an AI-powered coding assistant that helps developers understand, create, modify, test, and maintain software. Copilot Agent mode goes beyond inline code completion. A developer can describe a high-level task in natural language, and the agent can inspect project context, plan steps, edit multiple files, run supported commands, review build or test output, and iterate.
This guide explains how to use GitHub Copilot Agent in an ASP.NET Core Web API application. It follows a practical Employee Management API example using C#, Entity Framework Core, and SQL Server. The same workflow can be adapted to ASP.NET Core MVC applications and Angular front ends.
Overview of GitHub Copilot Agent
Copilot Agent is intended for multi-step development work. Instead of asking for one code snippet, the developer gives the agent a goal and relevant constraints. The agent determines which files are relevant, proposes or makes changes, may run commands, and uses the resulting output to continue the task.
Typical activities include:
Understanding an existing solution and explaining its architecture.
Creating or updating controllers, services, models, DTOs, and configuration.
Implementing REST API endpoints and database access.
Generating unit tests and helping diagnose failing tests.
Refactoring code and identifying possible security, performance, and maintainability issues.
Updating documentation and creating project-specific coding guidance.
Important: Copilot is an assistant, not an automatic approval system. Developers remain responsible for reviewing generated code, commands, dependencies, security, and test results.
Prerequisites
Before getting started, ensure you have:
Visual Studio 2022 version 17.14 or later for Agent mode in Visual Studio.
A GitHub account with access to GitHub Copilot. Organization policies may affect available features.
.NET SDK compatible with the target project.
An existing ASP.NET Core solution or a new ASP.NET Core Web API project.
SQL Server and Entity Framework Core if the sample database workflow is followed.
A source-control repository and a clean or committed working tree before making larger changes.
Set Up GitHub Copilot in Visual Studio
Step 1: Open Copilot Chat
Open Visual Studio 2022 and load your ASP.NET Core solution.
Select the GitHub Copilot badge in the upper-right corner, then open the Copilot Chat window. Alternatively, use View → GitHub Copilot Chat.
Sign in with the GitHub account that has Copilot access. If Copilot is not installed, follow the Visual Studio installation prompt.
At the bottom of the chat window, open the mode selector and choose Agent.
Step 2: Understand the Available Modes
Mode | Purpose | When to use it |
|---|
Ask | Answers questions and explains code. | Understand a method, API, exception, or project structure. |
Plan | Helps break a larger requirement into implementation steps. | Review the approach before making broad changes, where available. |
Agent | Works through a task using project context, edits, and supported tools. | Implement a feature across multiple files, run tests, and iterate. |
The exact mode names and capabilities can vary by IDE version and Copilot configuration. In Visual Studio 2022, Agent mode is available in version 17.14 and later.
Practical Example: Employee Management Web API
The example uses an ASP.NET Core Web API with a layered structure. Ask Copilot to inspect the existing solution first, then request one focused implementation step at a time. This makes the output easier to review and reduces unnecessary changes.
Application Architecture
Layer | Responsibility |
|---|
Controllers | Expose HTTP endpoints and translate requests into service calls. |
Services | Contain business rules and application logic. |
Data / DbContext | Configure Entity Framework Core and database access. |
Models / DTOs | Represent persisted entities and API request/response contracts. |
SQL Server | Stores employee records. |
Step 1: Ask Agent to Analyze the Existing Solution
Start with a read-only analysis prompt. Do not ask it to change files until you understand the proposed architecture.
Copilot Agent Prompt – Project Analysis
Analyze my existing ASP.NET Core Web API solution.
Identify:
1. Project structure and architecture.
2. Controllers, services, models, and DTOs.
3. Dependency injection configuration.
4. Database provider and DbContext configuration.
5. Authentication and authorization approach.
6. Existing coding and testing conventions.
Do not modify any files. Summarize the application and
identify the best location for a new Employee feature.
This first step gives the agent enough context to understand how the existing application is organized.
It also reduces the risk of introducing a new architectural pattern that conflicts with the project.
Step 2: Ask Agent to Plan the Feature
Before asking Copilot to modify multiple files, ask it to produce an implementation plan.
Copilot Agent Prompt – Implementation Plan
Plan an Employee Management feature for this solution.
Requirements:
- Employee create, read, update, and delete operations.
- Entity Framework Core and the existing SQL Server setup.
- Request and response DTOs with validation.
- Service interface and implementation.
- RESTful controller endpoints.
- Unit tests using the test framework already in the solution.
First provide the files to be created or updated, the
implementation sequence, and any assumptions. Do not
change files until I approve the plan.
A good plan should identify the existing files that can be reused and the files that actually need to be created or modified.
This is particularly useful in established applications where adding another service, repository, or configuration class may be unnecessary.
Step 3: Implement the Employee Model and DTOs
Once the plan has been reviewed, ask Agent to implement the model and API contracts.
Copilot Agent Prompt – Model and DTOs
Implement the Employee entity and API DTOs using the
existing project conventions.
Employee fields:
- Id: int
- Name: required string
- Email: required valid email
- Department: optional string
- Salary: decimal
- CreatedDate: DateTime
Create separate request and response DTOs. Add suitable
validation attributes. Avoid exposing persistence entities
directly from API endpoints. Do not duplicate existing types.
A generated entity may look like the following. Review nullability, validation, naming, and business requirements before accepting it.
Example C# Employee Entity
using System.ComponentModel.DataAnnotations;
public class Employee
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
[EmailAddress]
public string Email { get; set; } = string.Empty;
public string? Department { get; set; }
public decimal Salary { get; set; }
public DateTime CreatedDate { get; set; }
}
The exact model should follow the conventions already used in the application. For example, if the project uses Fluent API configuration instead of data annotations, Copilot should follow that pattern rather than introducing a different approach.
Step 4: Implement Data Access and Service Layer
Business logic should generally remain outside the controller. Ask Agent to work with the application's existing data-access architecture.
Copilot Agent Prompt – Service Layer
Create IEmployeeService and EmployeeService.
Implement asynchronous methods:
- GetAllAsync()
- GetByIdAsync(int id)
- CreateAsync(EmployeeRequest request)
- UpdateAsync(int id, EmployeeRequest request)
- DeleteAsync(int id)
Use the existing DbContext and DTOs. Keep business logic
out of the controller. Handle missing records consistently.
Use cancellation tokens if that matches project conventions.
Do not add a new repository abstraction unless the existing
architecture uses one.
The final point is important. Developers should not ask Copilot to add repositories, generic repositories, or additional abstractions simply because they are common patterns.
The existing architecture should determine whether another abstraction is actually required.
Step 5: Create the API Controller
The controller should remain focused on HTTP concerns such as routing, request validation, status codes, and calling the service layer.
Copilot Agent Prompt – Controller
Create EmployeesController using IEmployeeService.
Endpoints:
GET /api/employees
GET /api/employees/{id}
POST /api/employees
PUT /api/employees/{id}
DELETE /api/employees/{id}
Use attribute routing, async actions, request validation,
appropriate HTTP status codes, and the project's existing
error-handling approach. Keep the controller thin.
Update dependency injection only where necessary.
Employee API Endpoints
HTTP method | Endpoint | Purpose |
|---|
GET | /api/employees
| Retrieve employees. |
GET | /api/employees/{id}
| Retrieve one employee. |
POST | /api/employees
| Create an employee. |
PUT | /api/employees/{id}
| Update an employee. |
DELETE | /api/employees/{id}
| Delete an employee. |
The agent should also follow the existing API conventions for response types, error responses, validation, and authentication.
Configure Entity Framework Core and SQL Server
Ask Agent to inspect the existing configuration before adding packages or changing Program.cs.
Avoid overwriting connection strings, authentication, logging, or other registrations already used by the application.
Copilot Agent Prompt – Database Configuration
Review my current Entity Framework Core and SQL Server
configuration.
If EmployeeDbContext is not already registered:
- Add the required DbSet and entity configuration.
- Register the DbContext using the existing configuration style.
- Check whether the SQL Server provider package is present.
- Explain the migration commands required.
Do not place credentials in source code. Do not run database
updates or destructive migrations without asking me first.
Typical EF Core migration commands can be run from the appropriate project directory:
dotnet ef migrations add AddEmployee
dotnet ef database update
Check the generated migration before applying it, especially when working against a shared, staging, or production database.
A migration should be treated as a database change that needs review, not simply as another generated source file.
Generate and Run Unit Tests
Copilot Agent can draft tests and use test output to help correct failures. The developer should confirm that the tests verify meaningful behavior and do not merely repeat the implementation.
Copilot Agent Prompt – Tests
Create unit tests for EmployeeService using the test framework
and conventions already present in this solution.
Cover:
- Returning all employees.
- Returning an employee by ID.
- Handling an employee that does not exist.
- Creating an employee.
- Updating an existing employee.
- Deleting an existing employee.
Include positive and negative cases. Use suitable test data
and mocks or an isolated database test approach. Run the
tests and summarize any failures.
Run the tests from the command line:
dotnet test
Review the tests rather than accepting them solely because they pass.
A useful test should verify behavior, including important edge cases, rather than simply checking that a method returns the value produced by the same implementation logic.
Build, Debug, and Iterate
After implementation, ask Agent to build the solution and investigate actual compiler or test output.
Avoid asking it to suppress warnings or change unrelated code merely to obtain a successful build.
Copilot Agent Prompt – Build and Diagnostics
Build the solution and run the relevant tests.
For each failure:
1. Identify the root cause from the actual output.
2. Explain which files need changes.
3. Apply the smallest appropriate fix.
4. Rebuild and rerun the affected tests.
Do not hide errors, remove tests, disable nullable checks,
or make unrelated changes. Report the final build and test
results, including any remaining warnings.
This workflow keeps debugging tied to actual compiler and test evidence.
It also prevents a common problem with AI-assisted development: changing unrelated code until the immediate error disappears.
Add Repository-Specific Copilot Instructions
A repository instruction file gives Copilot reusable context about the application's architecture and coding standards.
Create the following file at the repository root:
.github/copilot-instructions.md
Example Copilot Instructions
# ASP.NET Core Project Instructions
## Technology
- Use the .NET version and packages already configured.
- Use C# and ASP.NET Core Web API.
- Use Entity Framework Core and the configured database.
## Architecture and coding
- Follow the existing solution architecture.
- Follow SOLID principles and use dependency injection.
- Keep controllers focused on HTTP concerns.
- Use DTOs for API request and response contracts.
- Use async/await for asynchronous I/O.
- Use meaningful names and avoid duplicate logic.
## Security and reliability
- Validate all incoming requests.
- Do not hard-code secrets or credentials.
- Do not log passwords, tokens, or sensitive data.
- Follow existing authentication and authorization rules.
- Use the established exception-handling approach.
## Testing
- Follow the test framework already used by the solution.
- Add positive and negative test cases.
- Run relevant tests after implementation.
- Do not remove or weaken tests to make a build pass.
Keep instructions concise, accurate, and specific to the repository.
Update them when the architecture or conventions change.
Repository instructions should guide Copilot rather than attempt to describe every implementation detail in the application.
Using Copilot Agent in an Existing Application
For an established application, begin with analysis rather than asking Copilot to rebuild the architecture.
A safe workflow is:
Commit or otherwise back up the current working changes.
Ask Copilot to explain the relevant feature and trace the request flow.
Identify the exact behavior to change and the files likely involved.
Ask for a plan and define what must not change.
Implement one small feature or defect fix at a time.
Inspect the diff, build output, tests, and runtime behavior.
Run security and regression checks before merging.
This approach is useful because existing applications often contain dependencies and conventions that are not obvious from a single source file.
Useful Copilot Agent Prompts for .NET Developers
Task | Example prompt |
|---|
Debugging | Trace this exception to its root cause. Use the stack trace and related code; propose the smallest safe fix. |
Refactoring | Refactor this service for readability and testability without changing its public behavior. |
Performance | Review this EF Core query for unnecessary database calls, tracking overhead, and inefficient loading. |
Security review | Review this API path for authorization gaps, input validation, injection risks, and sensitive logging. |
API documentation | Add or improve OpenAPI documentation for these endpoints, including request and response examples. |
Angular integration | Inspect the API contracts and create Angular service methods and components that consume these endpoints. |
Code review | Review the current changes for correctness, security, performance, maintainability, and missing tests. |
These prompts work better when they include the relevant scope and constraints instead of asking Copilot to "improve" the entire application.
Best Practices and Important Considerations
Give Agent a clear goal, relevant context, constraints, and acceptance criteria.
Prefer small, reviewable tasks over one very broad instruction.
Ask for a plan before large refactoring or architectural changes.
Review every file diff. Generated code can be incomplete or incorrect.
Approve terminal commands deliberately, especially migrations, package changes, file deletion, and deployment commands.
Never paste production secrets, access tokens, passwords, or confidential customer data into prompts.
Run builds, unit tests, integration tests, and security checks yourself.
Keep source-control commits small so changes can be traced and reverted.
Important: Agent mode does not guarantee correct or secure code. Authentication and authorization must be enforced by the server. A successful build does not prove that business rules, data integrity, or security requirements are satisfied.
Conclusion
GitHub Copilot Agent can accelerate ASP.NET Core development by helping developers understand existing solutions, plan features, generate code across multiple files, create tests, and investigate build or test failures.
The most effective approach is to provide clear project context, use focused prompts, review each proposed change, and verify the result through builds and tests.
For the Employee Management example, the workflow covered project analysis, planning, entity and DTO creation, service and controller implementation, database configuration, unit testing, and validation.
The same approach can be reused for real-world Web API features and Angular-integrated applications. The important part is to treat Copilot Agent as a development assistant while keeping architectural decisions, security, testing, and final code review under developer control.
Join the conversation! Your thoughts help the community grow.