InvalidOperationException: The ConnectionString property has not been initialized
The database connection was opened with an empty connection string — the configuration key wasn’t found.
Seen on:
.NET
Meaning
GetConnectionString("Default") returns null when the name doesn’t match appsettings.json, the file isn’t copied to output, the environment-specific file is missing, or the environment variable uses the wrong format (ConnectionStrings__Default).
Common causes
- Name mismatch between code and ConnectionStrings section
- appsettings.json not copied to output / wrong ASPNETCORE_ENVIRONMENT
- Env var not using double underscores
- Secret stored only in user-secrets on another machine
⚡ Quick fix
- Log builder.Configuration.GetConnectionString("Default") at startup
- Match the name exactly
- Set ConnectionStrings__Default in the environment for production
Detailed fix by platform
C#
- csharp
var cs = builder.Configuration.GetConnectionString("Default") ?? throw new InvalidOperationException("ConnectionStrings:Default missing"); builder.Services.AddDbContext<AppDb>(o => o.UseSqlServer(cs));
How to diagnose
- Config — Value at startup
- Environment — ASPNETCORE_ENVIRONMENT
- File — Copied to bin?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in .NET
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- InvalidOperationException InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y'
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026