From 5af4a93f4fd318799e3de911622b34c663665a9d Mon Sep 17 00:00:00 2001 From: Brian Donaldson Date: Sun, 1 Jun 2025 00:23:16 -0500 Subject: [PATCH 1/2] Update app-connection-examples.html.markerb Add .NET docs for pg connection. --- .../app-connection-examples.html.markerb | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/postgres/connecting/app-connection-examples.html.markerb b/postgres/connecting/app-connection-examples.html.markerb index 2243770424..69f76abbf0 100644 --- a/postgres/connecting/app-connection-examples.html.markerb +++ b/postgres/connecting/app-connection-examples.html.markerb @@ -371,3 +371,27 @@ if (config.use_env_variable) { } ``` + +## Connecting with .NET - EF Core & Npgsql +[docs](https://www.npgsql.org/efcore/index.html?tabs=onconfiguring#configuring-the-project-file) + +Minimal setup using the `DATABASE_URL` environment variable provisioned automatically with attaching a pg app. In ```Program.cs```: + +```csharp +builder.Services.AddDbContext(options => +{ + var databaseUrl = Environment.GetEnvironmentVariable("DATABASE_URL"); + if (!string.IsNullOrEmpty(databaseUrl)) + { + var uri = new Uri(databaseUrl); + var userInfo = uri.UserInfo.Split(':'); + var username = userInfo[0]; + var password = userInfo.Length > 1 ? userInfo[1] : ""; + var host = uri.Host; + var port = uri.Port > 0 ? uri.Port.ToString() : "5432"; + var database = uri.AbsolutePath.TrimStart('/'); + var connectionString = $"Host={host};Port={port};Database={database};Username={username};Password={password};SSL Mode=Require;Trust Server Certificate=true"; + options.UseNpgsql(connectionString); + } +}); +``` From f6145bf3d93791c1cb908db53ce36249126ecd46 Mon Sep 17 00:00:00 2001 From: Brian Donaldson Date: Sun, 1 Jun 2025 00:32:37 -0500 Subject: [PATCH 2/2] Update app-connection-examples.html.markerb Update with uri error handling. --- .../app-connection-examples.html.markerb | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/postgres/connecting/app-connection-examples.html.markerb b/postgres/connecting/app-connection-examples.html.markerb index 69f76abbf0..474b908604 100644 --- a/postgres/connecting/app-connection-examples.html.markerb +++ b/postgres/connecting/app-connection-examples.html.markerb @@ -375,7 +375,7 @@ if (config.use_env_variable) { ## Connecting with .NET - EF Core & Npgsql [docs](https://www.npgsql.org/efcore/index.html?tabs=onconfiguring#configuring-the-project-file) -Minimal setup using the `DATABASE_URL` environment variable provisioned automatically with attaching a pg app. In ```Program.cs```: +Minimal parsing setup using the `DATABASE_URL` environment variable provisioned automatically with attaching a pg app. In ```Program.cs```: ```csharp builder.Services.AddDbContext(options => @@ -383,15 +383,38 @@ builder.Services.AddDbContext(options => var databaseUrl = Environment.GetEnvironmentVariable("DATABASE_URL"); if (!string.IsNullOrEmpty(databaseUrl)) { - var uri = new Uri(databaseUrl); - var userInfo = uri.UserInfo.Split(':'); - var username = userInfo[0]; - var password = userInfo.Length > 1 ? userInfo[1] : ""; - var host = uri.Host; - var port = uri.Port > 0 ? uri.Port.ToString() : "5432"; - var database = uri.AbsolutePath.TrimStart('/'); - var connectionString = $"Host={host};Port={port};Database={database};Username={username};Password={password};SSL Mode=Require;Trust Server Certificate=true"; - options.UseNpgsql(connectionString); + Uri uri; + + try + { + uri = new Uri(databaseUrl); + } + catch (UriFormatException ex) + { + throw new InvalidOperationException( + "The DATABASE_URL environment variable is not a valid URI.", + ex + ); + } + + var userInfo = uri.UserInfo.Split(':'); + var dbUserFromUrl = userInfo[0]; + var dbPwFromUrl = userInfo.Length > 1 ? userInfo[1] : ""; + var dbHostFromUrl = uri.Host; + var dbPortFromUrl = uri.Port > 0 ? uri.Port.ToString() : "5432"; + var dbNameFromUrl = uri.AbsolutePath.TrimStart('/'); + + // Extract sslmode if present + var sslMode = "Require"; + var query = HttpUtility.ParseQueryString(uri.Query); + if (!string.IsNullOrEmpty(query["sslmode"])) + { + sslMode = query["sslmode"]; + } + + var npgsqlConn = + $"Host={dbHostFromUrl};Port={dbPortFromUrl};Database={dbNameFromUrl};Username={dbUserFromUrl};Password={dbPwFromUrl};SSL Mode={sslMode};Trust Server Certificate=true"; + options.UseNpgsql(npgsqlConn); } }); ```