Pular para o conteúdo
Docs Try Aspire
Docs Try

Set up Azure SQL in the AppHost (EF Core)

Este conteúdo não está disponível em sua língua ainda.

Azure SQL Database logo

This article is the hosting integration reference for the Aspire Azure SQL EF Core integration set. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model Azure SQL server and database resources in your AppHost project.

If you’re new to the Azure SQL EF Core integration, start with the Get started with Azure SQL EF Core integrations guide. For how consuming C# apps connect using Entity Framework Core, see Connect to Azure SQL with EF Core. For the full cloud-focused hosting reference — including provisioned Bicep, admin deployment scripts, private endpoints, and multi-language connection examples — see Set up Azure SQL Database in the AppHost.

The Aspire Azure SQL Database hosting integration models the SQL server as the AzureSqlServerResource type and SQL databases as the AzureSqlDatabaseResource type. To access these types and APIs, install the 📦 Aspire.Hosting.Azure.Sql NuGet package in your AppHost project:

Terminal
aspire add azure-sql

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Azure.Sql@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Azure.Sql" Version="*" />

Add Azure SQL server resource and database resource

Section titled “Add Azure SQL server resource and database resource”

In your AppHost project, call AddAzureSqlServer to add and return an Azure SQL server resource builder. Chain a call to AddDatabase to add an Azure SQL database resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddAzureSqlServer("sql");
var db = sql.AddDatabase("database");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(db);
// After adding all resources, run the app...
builder.Build().Run();
  1. The AddAzureSqlServer (or addAzureSqlServer) call models an Azure SQL Server that is provisioned in Azure when you publish your app.

  2. Calling AddDatabase (or addDatabase) on the returned resource builder models a named database within that server.

  3. The WithReference (or withReference) call configures a named connection in the consuming project that matches the database resource name.

You might have an existing Azure SQL Database service that you want to connect to. Chain a call to AsExisting (or asExisting) to annotate that your resource already exists in Azure:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var existingName = builder.AddParameter("existingSqlServerName");
var existingResourceGroup = builder.AddParameter("existingSqlServerResourceGroup");
var sql = builder.AddAzureSqlServer("sql")
.AsExisting(existingName, existingResourceGroup)
.AddDatabase("database");
builder.AddProject<Projects.ExampleProject>()
.WithReference(sql);
// After adding all resources, run the app...
builder.Build().Run();

For more information on treating Azure SQL resources as existing resources, see Use existing Azure resources.

Run Azure SQL server resource as a container

Section titled “Run Azure SQL server resource as a container”

During local development and testing, you can run an Azure SQL server as a local SQL Server container instead of provisioning an actual Azure resource. Call RunAsContainer (or runAsContainer) to switch to a local container:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddAzureSqlServer("sql")
.RunAsContainer();
var db = sql.AddDatabase("database");
var exampleProject = builder.AddProject<Projects.ExampleProject>()
.WithReference(db);
// After adding all resources, run the app...
builder.Build().Run();

When RunAsContainer is active, Aspire pulls a SQL Server container image and runs it locally. The consuming project receives the same connection environment variables it would receive when deployed to Azure, so your code works without modification between local and cloud environments.

When you deploy an Azure SQL Server resource, Aspire runs a deployment script that grants your application’s managed identity access to the SQL database. For more information, including private endpoint considerations and how to customize the deployment script behavior, see Admin deployment script.