Databases and Redis · Troubleshooting walkthrough
SqlConnection.Open throws InvalidOperationException about an uninitialized ConnectionString
Short answer
InvalidOperationException: The ConnectionString property has not been initialized means SqlConnection reached Open with an empty connection string. Usually GetConnectionString returned null because the name or settings file did not match, the production server lacks the value, or the code reopened a connection that was already disposed. Fail fast on null, and create one SqlConnection per operation.
SqlConnection.Open throws this when the connection object holds no connection string at all. It is not a bad-credentials or unreachable-server error: SqlClient never got as far as trying to connect. The empty value has three common sources, and they are quick to tell apart.
Work from the configuration backwards. First prove a string reaches the line that opens the connection, then check that the same key exists where the app runs, and only then look at how long the connection object lives.
The text you see
System.InvalidOperationException: The ConnectionString property has not been initialized.
Stage 1
Match the names the code asks for with the names in the settings
List every place the app looks up a connection string next to the keys defined in the JSON files. A name that exists on only one side is the first suspect.
Lookups and definitions side by side
grep -rnE 'GetConnectionString|ConnectionStrings' --include=*.cs --include='appsettings*.json' .Stage 2
Stage 1: the lookup returned null
GetConnectionString("Name") is a shorthand for reading ConnectionStrings:Name. Microsoft's reference for it and the configuration guide agree that a missing key gives null, not an exception, so the null travels on until Open rejects it. The usual culprits are "Default" in code against "DefaultConnection" in the file, or a key placed outside the ConnectionStrings section.
Stop the app at startup when the value is missing, so the failure names the setting instead of a database call.
Fail fast with your own message
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("ConnectionStrings:DefaultConnection is missing.");Stage 3
Stage 2: it works locally and fails after publishing
ASP.NET Core loads appsettings.{Environment}.json for the current environment only, and reads user secrets only in Development. A string kept in either place does not exist in Production. On a server or in a container, supply the same key as an environment variable, where a double underscore stands in for the colon.
Check the variable, then set it for the run
printenv | grep -i connectionstrings
export ConnectionStrings__DefaultConnection="Server=db.example.com;Database=app;User ID=app;Password=<secret>;Encrypt=True"
dotnet MyApp.dllKeep production secrets out of source control; use the host's secret store or protected environment settings.
Stage 4
Stage 3: the first query works and a later one fails
If the failure follows a successful call, suspect a connection object that outlived its using block. In the Microsoft.Data.SqlClient source, Dispose clears the stored connection options, so a later Open on the same object reports the not-initialized error. A SqlConnection held in a static field or a long-lived service is the typical shape.
Microsoft's pooling guide recommends opening late and closing early; pooling reuses the physical connections, so creating one SqlConnection per operation is cheap.
Find shared connections, then use one per operation
grep -rnE 'static .*SqlConnection|SqlConnection _' --include=*.cs .
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync(cancellationToken);
using var command = new SqlCommand(sql, connection);
await command.ExecuteNonQueryAsync(cancellationToken);Stage 5
Start the app and repeat the failing action
- Start the app; a missing key now stops it with your message.
- On the published host, hit an endpoint that queries the database.
- Repeat the failing action two or three times in a row: every call should succeed.
If the string is present and the failure persists, log the string's length just before Open; an empty value can come from a variable that a deployment pipeline expanded to nothing.
If you only keep one thing
- Null in means this exception out: check the key name first.
- Environment files and user secrets do not travel to production.
- One short-lived SqlConnection per operation; never a shared one.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.