Skip to content

Add "Apply migrations" section to MVC validation tutorial#36817

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-migration-for-validation-rules
Draft

Add "Apply migrations" section to MVC validation tutorial#36817
Copilot wants to merge 2 commits intomainfrom
copilot/add-migration-for-validation-rules

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

The ASP.NET Core MVC validation tutorial (Part 9) was missing guidance on running an EF Core migration after adding validation attributes that affect the database schema — leaving the model and DB schema out of sync. The Razor Pages equivalent tutorial already had this section.

Changes

  • validation.md (≥ aspnetcore-10.0) and versioned includes for 6.0–9.0 (validation6.mdvalidation9.md): Added ## Apply migrations section after "Using DataType Attributes" and before "Additional resources"

The new section explains that [Required]NOT NULL and [StringLength(60)]nvarchar(60), and provides tabbed migration commands:

  • Visual Studio: incremental migration via PMC

    Add-Migration New_DataAnnotations
    Update-Database
  • Visual Studio Code: delete-and-recreate (required due to SQLite ALTER TABLE limitations, consistent with Part 8)

    dotnet ef migrations add InitialCreate
    dotnet ef database update
    
  • Updated ms.date and added ai-usage: ai-assisted to validation.md frontmatter

Original prompt

This section details on the original issue you should resolve

<issue_title>For consistency: add-migration after applying validation rules</issue_title>
<issue_description>It's worth mentioning that some validation rules must be applied to the DB schema for consistency by adding a new migration. e.g. add-migration AddingNewRules

Mainly, the following two attributes affect the schema:
1- [Required] will alter the relevant column and make it not null
2- [StringLength(60)] will alter the type of the column and make it nvarchar(60)


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@wadepickett ## 🤖 AI Triage Summary

📌 Note to community: This is an automated preliminary analysis to help our documentation team quickly review, determine scope and prioritize this issue. This report is not a resolution or answer to your question—it's an internal triage tool that identifies potentially relevant docs, code samples, and versions to look into. A team member will review this issue and respond. Thank you for your contribution!


This preliminary assessment report was run by: @wadepickett
Date: 2026-02-27
Issue: 10429
Model: GitHub Copilot


Issue Analysis: Add migration step after applying validation rules for database schema consistency

✅ Issue Validation

Status: Valid and actionable

📋 Issue Summary

The issue requests that the Part 9 "Add validation" tutorial mention running an EF Core migration after adding validation attributes that affect the database schema—specifically [Required] (makes columns NOT NULL) and [StringLength(60)] (changes columns to nvarchar(60)).

After a careful walkthrough of all 10 parts of the tutorial series, the issue's core observation is confirmed:

Tutorial Schema Evolution (verified)

Tutorial Part Model Change Migration Created? DB Schema After
Part 4 (adding-model) Initial Movie class created (no validation attrs) InitialCreate Title nvarchar(max) NULL, Genre nvarchar(max) NULL
Part 6 (controller-methods-views) Adds [Display], [DataType], [Column(TypeName)] ❌ No migration Same — [Display]/[DataType] don't affect schema; [Column] already matched
Part 8 (new-field) Adds Rating property Add-Migration Rating Adds Rating nvarchar(max) NULL column
Part 9 (validation) Adds [Required], [StringLength(60)], [StringLength(30)], [StringLength(5)], [RegularExpression], [Range] No migration Schema unchanged — columns remain nvarchar(max), NULL despite model constraints

Result: After Part 9, the application enforces validation at the client and server level, but the database schema does not enforce NOT NULL or length constraints. This is the exact gap the issue author identified.

Why the tutorial "works" without the migration

Part 9 begins with a "Delete the previously edited data" section (Line 30-33) that instructs users to delete all movies so seed data can repopulate. This prevents runtime exceptions from existing NULL data conflicting with [Required], but it does not update the database schema itself. The app-level validation prevents bad data from reaching the database, so the tutorial "works" — but the schema and model are out of sync.

📁 Potentially Affected Files

These files have been identified as possibly related to this issue and may need review.

File Path Lines Section
Main article (10.0) aspnetcore/tutorials/first-mvc-app/validation.md 57-62 After validation attribute descriptions, before "Validation Error UI"
Versioned include (9.0) aspnetcore/tutorials/first-mvc-app/validation/includes/validation9.md Equivalent section
Versioned ...

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…rted versions

Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com>
Copilot AI changed the title [WIP] Add migration for applying validation rules to schema Add "Apply migrations" section to MVC validation tutorial Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

For consistency: add-migration after applying validation rules

2 participants