Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/frontend/config/sidebar/docs.topics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ export const docsTopics = {
'zh-CN': '数据与存储',
},
items: [
{
label: 'Database patterns',
slug: 'fundamentals/database-patterns',
},
{
label: 'Persist data with volumes',
slug: 'fundamentals/persist-data-volumes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,5 +512,5 @@ await postgres.withDataVolume();
For **caches or other ephemeral state**, `WithPersistentLifetime()` alone may be sufficient because losing data on container recreation is acceptable.

:::tip
For more details on volumes and bind mounts, see [Persist data using volumes](/fundamentals/persist-data-volumes/).
For more details on volumes and bind mounts, see [Persist data using volumes](/fundamentals/persist-data-volumes/). For how persistence affects database seed data and initialization scripts, see [Persist a database across restarts](/fundamentals/database-patterns/#persist-a-database-across-restarts).
:::
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import customClearCacheCommandSucceeded from '@assets/fundamentals/custom-clear-
import customClearCacheCommandSucceededLight from '@assets/fundamentals/custom-clear-cache-command-succeeded-light.png';
import ApiReference from '@components/ApiReference.astro';

Each resource in the Aspire app model is represented as an `IResource` and when added to the distributed application builder, it's the generic-type parameter of the `IResourceBuilder<T>` interface. You use the _resource builder_ API to chain calls, configuring the underlying resource, and in some situations, you might want to add custom commands to the resource. Some common scenario for creating a custom command might be running database migrations or seeding/resetting a database. In this article, you learn how to add a custom command to a Redis resource that clears the cache.
Each resource in the Aspire app model is represented as an `IResource` and when added to the distributed application builder, it's the generic-type parameter of the `IResourceBuilder<T>` interface. You use the _resource builder_ API to chain calls, configuring the underlying resource, and in some situations, you might want to add custom commands to the resource. Some common scenario for creating a custom command might be running database migrations or seeding/resetting a database. For database examples, see [Aspire database patterns](/fundamentals/database-patterns/). In this article, you learn how to add a custom command to a Redis resource that clears the cache.

<Aside type="caution">
These [Aspire dashboard](/dashboard/overview/) commands are only available
Expand Down Expand Up @@ -1635,6 +1635,8 @@ await builder.build().run();

In C#, the callback overload receives an `ExecuteCommandContext`. In TypeScript, `withProcessCommandFactory` receives the same execution context and returns the process specification. The dashboard renders the configured arguments as a prompt dialog before starting the process, and the entered values are available through the command context.

To run a seed program that needs a database's connection information, you can add it as a resource instead of a command. For an example, see [Seed a database in a dashboard REPL terminal](/fundamentals/database-patterns/#seed-a-database-in-a-dashboard-repl-terminal).

### `ProcessCommandSpec` properties

The following configuration options are available for the process to run. In C#, these map to `ProcessCommandSpec` properties. In TypeScript, provide them as fields in the options object passed to `withProcessCommand`, or return them from the `withProcessCommandFactory` callback.
Expand Down
2,071 changes: 2,071 additions & 0 deletions src/frontend/src/content/docs/fundamentals/database-patterns.mdx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import customHttpCommandSucceeded from '@assets/fundamentals/custom-http-command
import customHttpCommandSucceededLight from '@assets/fundamentals/custom-http-command-succeeded-light.png';
import ApiReference from '@components/ApiReference.astro';

In Aspire, you can add custom HTTP commands to resources using the <ApiReference name="Aspire.Hosting.ResourceBuilderExtensions.WithHttpCommand" /> API. This API extends existing functionality, where you provide [custom commands on resources](/fundamentals/custom-resource-commands/). This feature enables a command on a resource that sends an HTTP request to a specified endpoint and path. This is useful for scenarios such as triggering database migrations, clearing caches, or performing custom actions on resources through HTTP requests.
In Aspire, you can add custom HTTP commands to resources using the <ApiReference name="Aspire.Hosting.ResourceBuilderExtensions.WithHttpCommand" /> API. This API extends existing functionality, where you provide [custom commands on resources](/fundamentals/custom-resource-commands/). This feature enables a command on a resource that sends an HTTP request to a specified endpoint and path. This is useful for scenarios such as triggering database migrations, clearing caches, or performing custom actions on resources through HTTP requests. For examples that seed a database and apply migrations, see [Aspire database patterns](/fundamentals/database-patterns/).

To implement custom HTTP commands, you define a command on a resource and a corresponding HTTP endpoint that handles the request. This article provides an overview of how to create and configure custom HTTP commands in Aspire.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,4 @@ You can apply the volume concepts in the preceding code to a variety of services
- [Tutorial: Connect an ASP.NET Core app to SQL Server using Aspire and Entity Framework Core](/integrations/databases/efcore/sql-server/sql-server-get-started/)
- [Aspire orchestration overview](/get-started/app-host/)
- [Persistent volumes on Kubernetes](/deployment/kubernetes/persistent-volumes/)
- [Aspire database patterns](/fundamentals/database-patterns/#persist-a-database-across-restarts)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Since Aspire projects use a containerized architecture, databases are ephemeral

In this article, you learn how to configure Aspire projects to run EF Core migrations during app startup. Before continuing, ensure your development environment is set up — see [Prerequisites](/get-started/prerequisites/).

For a comparison of the ways to create, seed, and migrate a database with EF Core or Drizzle, see [Aspire database patterns](/fundamentals/database-patterns/?orm=efcore).

## Obtain the starter app

This tutorial uses a sample app that demonstrates how to apply EF Core migrations in Aspire. Use Visual Studio to clone [the sample app from GitHub](https://github.com/MicrosoftDocs/aspire-docs-samples/) or use the following command:
Expand Down Expand Up @@ -209,7 +211,7 @@ Ensure you're running migration commands from the correct project:

## Create the migration service

To execute migrations, call the EF Core `Microsoft.EntityFrameworkCore.Migrations.IMigrator.Migrate` method or the `IMigrator.MigrateAsync` method. In this tutorial, you'll create a separate worker service to apply migrations. This approach separates migration concerns into a dedicated project, which is easier to maintain and allows migrations to run before other services start.
To execute migrations, call the EF Core `Microsoft.EntityFrameworkCore.Migrations.IMigrator.Migrate` method or the `IMigrator.MigrateAsync` method. In this tutorial, you'll create a separate worker service to apply migrations. This approach separates migration concerns into a dedicated project, which is easier to maintain and allows migrations to run before other services start. For a condensed version of this pattern with a PostgreSQL database, and for the alternatives, see [Apply schema migrations on startup](/fundamentals/database-patterns/?orm=efcore#apply-schema-migrations-on-startup).

:::note
**Where to create migrations**: Migrations should be created in the project that contains your Entity Framework DbContext and model classes (in this example, *SupportTicketApi.Data*). The migration service project references this data project to apply the migrations at startup.
Expand Down Expand Up @@ -549,6 +551,8 @@ The [Aspire Shop](https://learn.microsoft.com/samples/microsoft/aspire-samples/a

In addition to creating a dedicated worker service as described above, Aspire provides a first-class AppHost API for managing EF Core migrations: <ApiReference name="Aspire.Hosting.EFResourceBuilderExtensions.AddEFMigrations" />. This API integrates migration execution directly into the AppHost orchestration and the Aspire publishing pipeline, without requiring a separate migration service project.

This section describes the API. For C# and TypeScript AppHost examples that apply migrations on startup, from a dashboard command, and during deployment, see [Aspire database patterns](/fundamentals/database-patterns/?orm=efcore#modify-a-database-schema).

### Add the package

Add the [📦 Aspire.Hosting.EntityFrameworkCore](https://www.nuget.org/packages/Aspire.Hosting.EntityFrameworkCore) NuGet package to your AppHost project:
Expand Down Expand Up @@ -645,7 +649,7 @@ Key behaviors:

#### Preventing container restarts per environment

A migration bundle is idempotent (running it twice is safe), but different compute environments need explicit configuration to avoid restarting the container after it exits.
A migration bundle is idempotent (running it twice is safe), but different compute environments need explicit configuration to avoid restarting the container after it exits. To make a deployment wait for the migrations to succeed before it updates the app, see [Ensure successful migrations deployment](/fundamentals/database-patterns/?orm=efcore#ensure-successful-migrations-deployment).

<Tabs syncKey="deploy-target">
<TabItem label="Azure Container Apps">
Expand Down Expand Up @@ -683,6 +687,7 @@ Customize the generated manifest to use a `Job` workload or set `restartPolicy:

## See also

- [Aspire database patterns](/fundamentals/database-patterns/?orm=efcore)
- [EF Core migrations](https://learn.microsoft.com/ef/core/managing-schemas/migrations/)
- [Connection resiliency](https://learn.microsoft.com/ef/core/miscellaneous/connection-resiliency)
- [Seed data in a database using Aspire](/integrations/databases/efcore/seed-database/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Aspire's EF Core integrations build on standard EF Core patterns and extend them
- **Pooled `DbContext` by default** — `Add<DatabaseSystem>DbContext` registers a context pool automatically, reducing per-request allocation overhead.
- **Built-in observability** — Health checks, OpenTelemetry distributed tracing, and connection-retry resiliency are enabled out of the box.
- **Enrichment API** — When you need custom `AddDbContextPool` configuration, call `Enrich<DatabaseSystem>DbContext` afterwards to layer Aspire's defaults on top of your own setup.
- **Migrations support** — Run EF Core schema migrations at startup or publish them as container images for deployment. See [Apply EF Core migrations in Aspire](/integrations/databases/efcore/migrations/).
- **Migrations support** — Run EF Core schema migrations at startup or publish them as container images for deployment. See [Apply EF Core migrations in Aspire](/integrations/databases/efcore/migrations/) and [Aspire database patterns](/fundamentals/database-patterns/?orm=efcore).
- **.NET-only client code** — EF Core runs inside consuming .NET services; all `DbContext` registration code is C#.

## Supported EF Core integrations
Expand Down Expand Up @@ -770,6 +770,7 @@ Only the base context state is reset when it's returned to the pool. If you've m
- [Entity Framework Core documentation hub](https://learn.microsoft.com/ef/core)
- [Apply Entity Framework Core migrations in Aspire](/integrations/databases/efcore/migrations/)
- [Seed a database in Aspire](/integrations/databases/efcore/seed-database/)
- [Aspire database patterns](/fundamentals/database-patterns/?orm=efcore)
- [Get started with the SQL Server EF Core integration](/integrations/databases/efcore/sql-server/sql-server-get-started/)
- [Get started with the PostgreSQL EF Core integration](/integrations/databases/efcore/postgres/postgresql-get-started/)
- [Get started with the MySQL EF Core integration](/integrations/databases/efcore/mysql/mysql-get-started/)
Expand Down
Loading
Loading