Skip to content

Commit 929d8c4

Browse files
Merge pull request #3397 from dotnet/main
✅ Merge `main` into `live`
2 parents cad0612 + 72cf05f commit 929d8c4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

docs/database/ef-core-migrations.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ Now you've got some migrations to apply. Next, you'll create a migration service
7575
7676
## Create the migration service
7777
78-
To run the migrations at startup, you need to create a service that applies the migrations.
78+
To execute migrations, call the EF Core <xref:Microsoft.EntityFrameworkCore.Migrations.IMigrator.Migrate*> method or the <xref:Microsoft.EntityFrameworkCore.Migrations.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.
79+
80+
To create a service that applies the migrations:
7981
8082
1. Add a new Worker Service project to the solution. If using Visual Studio, right-click the solution in Solution Explorer and select **:::no-loc text="Add":::** > **:::no-loc text="New Project":::**. Select **:::no-loc text="Worker Service":::**, name the project *:::no-loc text="SupportTicketApi.MigrationService":::* and target **.NET 8.0**. If using the command line, use the following commands from the solution directory:
8183
@@ -132,6 +134,12 @@ The migration service is created, but it needs to be added to the .NET Aspire ap
132134
133135
This enlists the *:::no-loc text="SupportTicketApi.MigrationService":::* project as a service in the .NET Aspire app host.
134136
137+
> [!NOTE]
138+
> In the preceding code, the call to <xref:Aspire.Hosting.SqlServerBuilderExtensions.AddDatabase*> adds a representation of a SQL Server database to the .NET Aspire application model with a connection string. It *doesn't* create a database in the SQL Server container. To ensure that the database is created, the sample project calls the EF Core <xref:Microsoft.EntityFrameworkCore.Storage.IDatabaseCreator.EnsureCreated*> method from the support ticket API's *:::no-loc text="Program.cs":::* file.
139+
140+
> [!TIP]
141+
> The code creates the SQL Server container each time it runs and applies migrations to it. Data doesn't persist across debugging sessions and any new database rows you create during testing will not survive an app restart. If you would prefer to persist this data, add a data volume to your container. For more information, see [Add SQL Server resource with data volume](sql-server-entity-framework-integration.md#add-sql-server-resource-with-data-volume).
142+
135143
1. If the code cannot resolve the migration service project, add a reference to the migration service project in the AppHost project:
136144
137145
```dotnetcli

0 commit comments

Comments
 (0)