diff --git a/docs/database/ef-core-migrations.md b/docs/database/ef-core-migrations.md index ce0ab8e6ac..3e13e703e4 100644 --- a/docs/database/ef-core-migrations.md +++ b/docs/database/ef-core-migrations.md @@ -155,3 +155,7 @@ Now that the migration service is configured, run the app to test the migrations ## Get the code You can find the [completed sample app on GitHub](https://github.com/MicrosoftDocs/aspire-docs-samples/tree/solution/SupportTicketApi). + +## More sample code + +The [Aspire Shop](/samples/dotnet/aspire-samples/aspire-shop/) sample app uses this approach to apply migrations. See the `AspireShop.CatalogDbManager` project for the migration service implementation. diff --git a/docs/database/sql-server-integrations.md b/docs/database/sql-server-integrations.md index fd8bf14404..29d0606278 100644 --- a/docs/database/sql-server-integrations.md +++ b/docs/database/sql-server-integrations.md @@ -65,15 +65,23 @@ In the _:::no-loc text="Program.cs":::_ file of the _AspireSQLEFCore_ project, a This method accomplishes the following tasks: -- Registers a `TicketDbContext` with the DI container for connecting to the containerized Azure SQL Database. +- Registers a `TicketContext` with the DI container for connecting to the containerized Azure SQL Database. - Automatically enable corresponding health checks, logging, and telemetry. -## Migrate and seed the database +## Create the database -While developing locally, you need to create a database inside the SQL Server container. Update the _:::no-loc text="Program.cs":::_ file with the following code to automatically run Entity Framework migrations during startup. +While developing locally, you need to create a database inside the SQL Server container. Update the _:::no-loc text="Program.cs":::_ file with the following code: :::code language="csharp" source="snippets/tutorial/AspireSQLEFCore/AspireSQLEFCore/Program.cs" range="1-30" highlight="16-30"::: +The preceding code: + +- Checks if the app is running in a development environment. +- If it is, it retrieves the `TicketContext` service from the DI container and calls `Database.EnsureCreated()` to create the database if it doesn't already exist. + +> [!NOTE] +> Note that `EnsureCreated()` is not suitable for production environments, and it only creates the database as defined in the context. It doesn't apply any migrations. For more information on Entity Framework Core migrations in .NET Aspire, see [Apply Entity Framework Core migrations in .NET Aspire](ef-core-migrations.md). + ## Create the form The app requires a form for the user to be able to submit support ticket information and save the entry to the database.