The application is throwing the following error:
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ClientVersion'.
This occurs because the ClientVersion column was added to the DeviceEntity model, but the database migration hasn't been applied to the SQL Server database.
- Migration Created: A migration file
20251109202217_AddClientVersionToDevice.cswas created to add theClientVersioncolumn to theDevicestable - Migration Not Applied: The migration exists in the codebase but hasn't been applied to the database
- Duplicate Migration: There's also a duplicate migration
20251109202105_AddClientVersionToDevice.csthat should be removed
Run the automated migration script:
# From repository root
.\scripts\Apply-DatabaseMigrations.ps1 -ForceThis script will:
- ? Check for dotnet-ef tools (install if missing)
- ? Remove duplicate migrations
- ? Apply all pending migrations
- ? Verify success
If you prefer manual control:
dotnet tool install --global dotnet-efcd SecureBootDashboard.Apidotnet ef migrations listExpected output:
Build succeeded.
20251105093532_InitialCreate (Applied)
20251105101836_AddCertificateCollection (Applied)
20251107164106_AddFirmwareReleaseDateToDevice (Applied)
20251107233431_AddUEFISecureBootEnabledToDevice (Applied)
20251109202105_AddClientVersionToDevice (Pending) <-- Duplicate
20251109202217_AddClientVersionToDevice (Pending) <-- Keep this one
# Delete duplicate migration files manually
Remove-Item "Data\Migrations\20251109202105_AddClientVersionToDevice.cs"
Remove-Item "Data\Migrations\20251109202105_AddClientVersionToDevice.Designer.cs"dotnet ef database updateExpected output:
Build succeeded.
Applying migration '20251109202217_AddClientVersionToDevice'.
Done.
dotnet ef migrations listAll migrations should now show (Applied).
Connect to SQL Server and verify the column exists:
-- Check if ClientVersion column exists in Devices table
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Devices'
AND COLUMN_NAME = 'ClientVersion';Expected result:
COLUMN_NAME DATA_TYPE IS_NULLABLE
ClientVersion nvarchar YES
Restart the API and test the /api/Devices endpoint:
# Restart API
# Then test:
Invoke-RestMethod -Uri "https://localhost:5000/api/Devices" -Method GetShould return device list without errors.
Navigate to the web dashboard:
- Homepage should load without errors
- Device list should display
- Client version should appear in device details
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ClientVersion",
table: "Devices",
type: "nvarchar(max)",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ClientVersion",
table: "Devices");
}What it does:
- Adds a new
ClientVersioncolumn to theDevicestable - Column type:
nvarchar(max)(unlimited length string) - Nullable: Yes (allows NULL values for existing records)
The duplicate migration (20251109202105) was likely created first, but then another migration with the same name was generated. This can happen when:
- Running
dotnet ef migrations addmultiple times - Migrations being generated on different machines
- Git merge conflicts
Best practice: Remove the older duplicate to avoid confusion.
The migration uses the connection string from appsettings.json:
"ConnectionStrings": {
"SqlServer": "Server=SRVSQL;Database=SecureBootDashboard;Trusted_Connection=True;TrustServerCertificate=True"
}If you need to apply the migration to a different database:
dotnet ef database update --connection "Server=YOUR_SERVER;Database=YOUR_DB;Trusted_Connection=True;TrustServerCertificate=True"Solution:
dotnet tool install --global dotnet-efSolution:
# Clean and rebuild
dotnet clean
dotnet build
dotnet ef database updateSolution: The migration already exists - just apply it:
dotnet ef database updateCauses:
- SQL Server not running
- Incorrect server name in connection string
- Firewall blocking connection
- SQL Server service stopped
Solution:
# Check SQL Server service
Get-Service -Name "MSSQL*"
# If stopped, start it:
Start-Service -Name "MSSQLSERVER"Causes:
- Windows Authentication not enabled
- User doesn't have permissions
Solution: Grant user permissions in SQL Server Management Studio or use SQL Authentication:
"SqlServer": "Server=SRVSQL;Database=SecureBootDashboard;User Id=sa;Password=YourPassword;TrustServerCertificate=True"# Create migration
dotnet ef migrations add MyMigrationName
# Immediately apply it
dotnet ef database updateAdd migration application to your deployment pipeline:
# Example Azure DevOps task
- task: DotNetCoreCLI@2
displayName: 'Apply EF Migrations'
inputs:
command: 'custom'
custom: 'ef'
arguments: 'database update --project SecureBootDashboard.Api'# List migrations and check for pending ones
dotnet ef migrations listThe API already includes health checks. Monitor them:
GET https://localhost:5000/health
- Entity Framework Core Migrations: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/
- Client Version Tracking:
docs\CLIENT_VERSION_TRACKING.md - Database Schema:
docs\DATABASE_SCHEMA.md(if exists)
? Problem: ClientVersion column missing from database
? Cause: Migration not applied
? Solution: Run .\scripts\Apply-DatabaseMigrations.ps1 -Force
? Verification: Query database, test API, check dashboard
After applying the migration, the application should work correctly with client version tracking enabled.
Last Updated: 2025-01-10
Related Issue: Database schema out of sync with entity model