From efed37bf2167fd2a69b0a52ff73498d1498d0c3d Mon Sep 17 00:00:00 2001 From: Cam Soper Date: Mon, 9 Sep 2024 10:53:12 -0500 Subject: [PATCH] Corrections and clarifications (#1608) --- docs/database/ef-core-migrations.md | 4 ++++ docs/database/sql-server-integrations.md | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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.