From 865d9c00f60dd53aabadaf18c35de38677c50b33 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 26 Mar 2026 10:43:19 -0700 Subject: [PATCH] Add "Apply migrations" section to MVC validation tutorial (#36817) * Initial plan * Add Apply migrations section to MVC validation tutorial for all supported versions Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Apply suggestion from @wadepickett * Reinsert review note for Details and Delete methods * Update validation7.md with review note Added a note about reviewing the app and improving methods. * Reinsert review note for Details and Delete methods * Update validation9.md * Add link to Part 8 tutorial on EF Core migrations * Apply suggestions from code review Add links to new field tutorial per tdykstra review Co-authored-by: Wade Pickett * Apply suggestion from @wadepickett * Add Part 8 link to validation.md Additional resources; update EF Core migrations links in new-field tutorials Agent-Logs-Url: https://github.com/dotnet/AspNetCore.Docs/sessions/6b4f3ff0-45af-46b6-aafa-bde7f5bf725e Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> Co-authored-by: Wade Pickett --- .../tutorials/first-mvc-app/new-field.md | 4 +- .../new-field/includes/new-field6.md | 2 +- .../new-field/includes/new-field7.md | 2 +- .../new-field/includes/new-field8.md | 2 +- .../tutorials/first-mvc-app/validation.md | 37 ++++++++++++++++++- .../validation/includes/validation6.md | 34 +++++++++++++++++ .../validation/includes/validation7.md | 33 +++++++++++++++++ .../validation/includes/validation8.md | 36 +++++++++++++++++- .../validation/includes/validation9.md | 34 +++++++++++++++++ 9 files changed, 177 insertions(+), 7 deletions(-) diff --git a/aspnetcore/tutorials/first-mvc-app/new-field.md b/aspnetcore/tutorials/first-mvc-app/new-field.md index 70f62d3def4c..fe4efbb37494 100644 --- a/aspnetcore/tutorials/first-mvc-app/new-field.md +++ b/aspnetcore/tutorials/first-mvc-app/new-field.md @@ -5,7 +5,7 @@ author: wadepickett description: Part 8 of tutorial series on ASP.NET Core MVC. monikerRange: '>= aspnetcore-3.1' ms.author: wpickett -ms.date: 02/24/2026 +ms.date: 03/26/2026 uid: tutorials/first-mvc-app/new-field --- # Part 8, add a new field to an ASP.NET Core MVC app @@ -14,7 +14,7 @@ uid: tutorials/first-mvc-app/new-field :::moniker range=">= aspnetcore-10.0" -In this section [Entity Framework](/ef/core/get-started/aspnetcore/new-db) Migrations is used to: +In this section [Entity Framework Core migrations](/ef/core/managing-schemas/migrations/) is used to: * Add a new field to the model. * Migrate the new field to the database. diff --git a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field6.md b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field6.md index be197ea21114..f17c070d86c8 100644 --- a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field6.md +++ b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field6.md @@ -1,6 +1,6 @@ :::moniker range="= aspnetcore-6.0" -In this section [Entity Framework](/ef/core/get-started/aspnetcore/new-db) Code First Migrations is used to: +In this section [Entity Framework Core migrations](/ef/core/managing-schemas/migrations/) is used to: * Add a new field to the model. * Migrate the new field to the database. diff --git a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field7.md b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field7.md index 66346a16add7..c3f5e95c1609 100644 --- a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field7.md +++ b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field7.md @@ -1,6 +1,6 @@ :::moniker range="= aspnetcore-7.0" -In this section [Entity Framework](/ef/core/get-started/aspnetcore/new-db) Code First Migrations is used to: +In this section [Entity Framework Core migrations](/ef/core/managing-schemas/migrations/) is used to: * Add a new field to the model. * Migrate the new field to the database. diff --git a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field8.md b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field8.md index c6116f7a8944..459de339f080 100644 --- a/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field8.md +++ b/aspnetcore/tutorials/first-mvc-app/new-field/includes/new-field8.md @@ -1,6 +1,6 @@ :::moniker range="= aspnetcore-8.0" -In this section [Entity Framework](/ef/core/get-started/aspnetcore/new-db) Code First Migrations is used to: +In this section [Entity Framework Core migrations](/ef/core/managing-schemas/migrations/) is used to: * Add a new field to the model. * Migrate the new field to the database. diff --git a/aspnetcore/tutorials/first-mvc-app/validation.md b/aspnetcore/tutorials/first-mvc-app/validation.md index bc1f4cca350c..b789e8513a3a 100644 --- a/aspnetcore/tutorials/first-mvc-app/validation.md +++ b/aspnetcore/tutorials/first-mvc-app/validation.md @@ -1,10 +1,11 @@ --- title: Part 9, add validation to an ASP.NET Core MVC app +ai-usage: ai-assisted author: wadepickett description: Part 9 of tutorial series on ASP.NET Core MVC. monikerRange: '>= aspnetcore-3.1' ms.author: wpickett -ms.date: 01/22/2026 +ms.date: 03/26/2026 uid: tutorials/first-mvc-app/validation --- @@ -135,8 +136,42 @@ You can use the `DisplayFormat` attribute by itself, but it's generally a good i * The `DataType` attribute can enable MVC to choose the right field template to render the data (the `DisplayFormat` if used by itself uses the string template). +## Apply migrations + +Some validation attributes affect the database schema. For example, `[Required]` makes a column `NOT NULL` and `[StringLength(60)]` limits the column type to `nvarchar(60)`. Run a migration to keep the database schema consistent with the model. + +# [Visual Studio](#tab/visual-studio) + +From the **Tools** menu, select **NuGet Package Manager > Package Manager Console**. + +In the PMC, enter the following commands: + +```powershell +Add-Migration New_DataAnnotations +Update-Database +``` + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/RP-mvc-shared/sqlite-warn.md)] + +Delete the Migrations folder and the database file, and then run the following .NET CLI commands: + +```dotnetcli +dotnet ef migrations add InitialCreate +``` + +```dotnetcli +dotnet ef database update +``` + +For more information, see [Resetting all migrations](/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#resetting-all-migrations). + +--- + ## Additional resources +* [Part 8, Add a new field (EF Core migrations)](xref:tutorials/first-mvc-app/new-field) * [Working with Forms](xref:mvc/views/working-with-forms) * [Globalization and localization](xref:fundamentals/localization) * [Introduction to Tag Helpers](xref:mvc/views/tag-helpers/intro) diff --git a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation6.md b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation6.md index 28d9f9eb1741..81edaf2d9b3e 100644 --- a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation6.md +++ b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation6.md @@ -102,10 +102,44 @@ The following code shows combining attributes on one line: [!code-csharp[](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie60/Models/Movie.cs?name=AttrOneLine)] +## Apply migrations + +Some validation attributes affect the database schema. For example, `[Required]` makes a column `NOT NULL` and `[StringLength(60)]` limits the column type to `nvarchar(60)`. Run a migration to keep the database schema consistent with the model. + +# [Visual Studio](#tab/visual-studio) + +From the **Tools** menu, select **NuGet Package Manager > Package Manager Console**. + +In the PMC, enter the following commands: + +```powershell +Add-Migration New_DataAnnotations +Update-Database +``` + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/RP-mvc-shared/sqlite-warn.md)] + +Delete the Migrations folder and the database file, and then run the following .NET CLI commands: + +```dotnetcli +dotnet ef migrations add InitialCreate +``` + +```dotnetcli +dotnet ef database update +``` + +For more information, see [Resetting all migrations](/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#resetting-all-migrations). + +--- + In the next part of the series, we review the app and make some improvements to the automatically generated `Details` and `Delete` methods. ## Additional resources +* [Part 8, Add a new field (EF Core migrations)](xref:tutorials/first-mvc-app/new-field) * [Working with Forms](xref:mvc/views/working-with-forms) * [Globalization and localization](xref:fundamentals/localization) * [Introduction to Tag Helpers](xref:mvc/views/tag-helpers/intro) diff --git a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation7.md b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation7.md index b583d7355502..f4c4ba84feb8 100644 --- a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation7.md +++ b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation7.md @@ -102,10 +102,43 @@ The following code shows combining attributes on one line: [!code-csharp[](~/tutorials/first-mvc-app/start-mvc/sample/MvcMovie70/Models/Movie.cs?name=snippet_AttrOneLine)] +## Apply migrations + +Some validation attributes affect the database schema. For example, `[Required]` makes a column `NOT NULL` and `[StringLength(60)]` limits the column type to `nvarchar(60)`. Run a migration to keep the database schema consistent with the model. + +# [Visual Studio](#tab/visual-studio) + +From the **Tools** menu, select **NuGet Package Manager > Package Manager Console**. + +In the PMC, enter the following commands: + +```powershell +Add-Migration New_DataAnnotations +Update-Database +``` + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/RP-mvc-shared/sqlite-warn.md)] + +Delete the Migrations folder and the database file, and then run the following .NET CLI commands: + +```dotnetcli +dotnet ef migrations add InitialCreate +``` + +```dotnetcli +dotnet ef database update +``` + +For more information, see [Resetting all migrations](/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#resetting-all-migrations). +--- + In the next part of the series, we review the app and make some improvements to the automatically generated `Details` and `Delete` methods. ## Additional resources +* [Part 8, Add a new field (EF Core migrations)](xref:tutorials/first-mvc-app/new-field) * [Working with Forms](xref:mvc/views/working-with-forms) * [Globalization and localization](xref:fundamentals/localization) * [Introduction to Tag Helpers](xref:mvc/views/tag-helpers/intro) diff --git a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation8.md b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation8.md index 5841b9f2da55..c6a231835723 100644 --- a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation8.md +++ b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation8.md @@ -107,10 +107,44 @@ The following code shows combining attributes on one line: [!code-csharp[](~/tutorials/first-mvc-app/start-mvc/sample/mvcmovie80/Models/Movie.cs?name=snippet_AttrOneLine)] +## Apply migrations + +Some validation attributes affect the database schema. For example, `[Required]` makes a column `NOT NULL` and `[StringLength(60)]` limits the column type to `nvarchar(60)`. Run a migration to keep the database schema consistent with the model. + +# [Visual Studio](#tab/visual-studio) + +From the **Tools** menu, select **NuGet Package Manager > Package Manager Console**. + +In the PMC, enter the following commands: + +```powershell +Add-Migration New_DataAnnotations +Update-Database +``` + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/RP-mvc-shared/sqlite-warn.md)] + +Delete the Migrations folder and the database file, and then run the following .NET CLI commands: + +```dotnetcli +dotnet ef migrations add InitialCreate +``` + +```dotnetcli +dotnet ef database update +``` + +For more information, see [Resetting all migrations](/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#resetting-all-migrations). + +--- + In the next part of the series, we review the app and make some improvements to the automatically generated `Details` and `Delete` methods. ## Additional resources +* [Part 8, Add a new field (EF Core migrations)](xref:tutorials/first-mvc-app/new-field) * [Working with Forms](xref:mvc/views/working-with-forms) * [Globalization and localization](xref:fundamentals/localization) * [Introduction to Tag Helpers](xref:mvc/views/tag-helpers/intro) @@ -120,4 +154,4 @@ In the next part of the series, we review the app and make some improvements to > [Previous](~/tutorials/first-mvc-app/new-field.md) > [Next](~/tutorials/first-mvc-app/details.md) -:::moniker-end \ No newline at end of file +:::moniker-end diff --git a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation9.md b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation9.md index 7f5ba7889230..d445bc2ed6cb 100644 --- a/aspnetcore/tutorials/first-mvc-app/validation/includes/validation9.md +++ b/aspnetcore/tutorials/first-mvc-app/validation/includes/validation9.md @@ -107,10 +107,44 @@ The following code shows combining attributes on one line: [!code-csharp[](~/tutorials/first-mvc-app/start-mvc/sample/mvcmovie90/Models/Movie.cs?name=snippet_AttrOneLine)] +## Apply migrations + +Some validation attributes affect the database schema. For example, `[Required]` makes a column `NOT NULL` and `[StringLength(60)]` limits the column type to `nvarchar(60)`. Run a migration to keep the database schema consistent with the model. + +# [Visual Studio](#tab/visual-studio) + +From the **Tools** menu, select **NuGet Package Manager > Package Manager Console**. + +In the PMC, enter the following commands: + +```powershell +Add-Migration New_DataAnnotations +Update-Database +``` + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/RP-mvc-shared/sqlite-warn.md)] + +Delete the Migrations folder and the database file, and then run the following .NET CLI commands: + +```dotnetcli +dotnet ef migrations add InitialCreate +``` + +```dotnetcli +dotnet ef database update +``` + +For more information, see [Resetting all migrations](/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#resetting-all-migrations). + +--- + In the next part of the series, we review the app and make some improvements to the automatically generated `Details` and `Delete` methods. ## Additional resources +* [Part 8, Add a new field (EF Core migrations)](xref:tutorials/first-mvc-app/new-field) * [Working with Forms](xref:mvc/views/working-with-forms) * [Globalization and localization](xref:fundamentals/localization) * [Introduction to Tag Helpers](xref:mvc/views/tag-helpers/intro)