Feature 027: PostgreSQL for Local Development#31
Merged
Conversation
Add NuGet packages and configure Aspire AppHost to use PostgreSQL container with pgAdmin instead of SQL Server. SQL Server configuration remains in code as comments for easy switching. Changes: - Add Aspire.Hosting.PostgreSQL package to AppHost - Add Npgsql.EntityFrameworkCore.PostgreSQL to Infrastructure - Configure PostgreSQL container with pgAdmin in AppHost - Comment out SQL Server container configuration - Keep connection name as "sql" for minimal code changes The variable name 'sql' now points to PostgreSQL, allowing all existing service references to work without modification.
Update API service to use Npgsql provider and PostgreSQL database while maintaining SQL Server configuration as comments for easy switching. Changes: - Add Aspire.Npgsql package to API project - Update DatabaseConfig to use UseNpgsql instead of UseSqlServer - Update Program.cs to use AddNpgsqlDataSource instead of AddSqlServerClient - Comment out SQL Server configuration with clear markers Connection name remains "sql" for consistency with Aspire AppHost.
Update all 4 microservices (Breeding, Racing, Training, Feeding) to use Npgsql provider and PostgreSQL database while maintaining SQL Server configuration as comments for easy switching. Changes per service: - Add Aspire.Npgsql package to project file - Update Program.cs to use UseNpgsql instead of UseSqlServer - Update Program.cs to use AddNpgsqlDataSource instead of AddSqlServerClient - Comment out SQL Server configuration with clear markers Connection name remains "sql" for consistency with Aspire AppHost. All services now follow the same dual-provider pattern.
Change GetConnectionString from "TripleDerby" to "sql" to match the Aspire resource name defined in AppHost. Aspire injects connection strings based on the resource name, not the database name. This fixes the "ConnectionString is missing" error when services attempt to connect to the database. Files updated: - API DatabaseConfig.cs - All 4 microservices Program.cs files
Removed builder.AddNpgsqlDataSource() calls from all services since
we're using manual DbContext configuration to maintain the dual-provider
pattern (PostgreSQL/SQL Server switching via comments).
Aspire automatically injects the connection string via .WithReference(sql)
in the AppHost, making it available through GetConnectionString("sql").
The manual UseNpgsql() configuration in each service's Program.cs uses
this connection string directly.
This approach allows us to maintain commented SQL Server code alongside
active PostgreSQL code without Aspire client library conflicts.
Files updated:
- API Program.cs
- All 4 microservices Program.cs files
Removed Aspire.Npgsql packages that were conflicting with manual DbContext configuration approach. The manual configuration is needed to maintain the dual-provider pattern (PostgreSQL active, SQL Server commented) for easy switching between database providers. Aspire connection strings are injected via .WithReference(sql) in AppHost and available through the ServiceDefaults configuration. Services updated: - TripleDerby.Api - TripleDerby.Services.Breeding - TripleDerby.Services.Racing - TripleDerby.Services.Training - TripleDerby.Services.Feeding
Separated Postgres server resource from database resource in AppHost to properly expose the database connection string. Changed connection string key from "sql" to "TripleDerby" to match the database resource name.
Changes:
- AppHost: Split AddPostgres and AddDatabase calls
- Postgres server named "sql", database named "TripleDerby"
- Updated all services to use GetConnectionString("TripleDerby")
This follows Aspire's resource naming convention where the connection string key matches the database resource name, not the server resource name.
Removed the unused 'hasError' field from Races, Horses, and RaceRuns components. Error state is tracked via the 'errorMessage' string instead. Fixes compiler warnings: - CS0414: The field 'Races.hasError' is assigned but its value is never used - CS0414: The field 'Horses.hasError' is assigned but its value is never used - CS0414: The field 'RaceRuns.hasError' is assigned but its value is never used
Updated feature 027 spec to reflect actual implementation with key decisions and troubleshooting guide. Created comprehensive DATABASE_SWITCHING.md guide for switching between PostgreSQL and SQL Server. Key documentation: - Implementation summary with architecture diagram - Step-by-step switching instructions - Connection string flow explanation - Troubleshooting common issues - File-by-file change reference Also includes minor UI enhancement to Horses page (ShowHover and Gender column search).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enables PostgreSQL as the primary database for local development while maintaining SQL Server configuration as commented code for easy switching. Includes .NET Aspire orchestration with PostgreSQL and pgAdmin containers.
Implementation
Key Decisions
postgres.AddDatabase("TripleDerby")following Aspire patternGetConnectionString("TripleDerby")matching database nameUseNpgsql()calls instead of Aspire auto-configurationFiles Modified
Testing
Switching Providers
See DATABASE_SWITCHING.md for detailed instructions on switching between PostgreSQL and SQL Server.
Quick Reference: 10 files to modify via commenting/uncommenting paired blocks
Related