# InvalidOperationException: The ConnectionString property has not been initialized

**Code:** `ConnectionString property has not been initialized` · **Category:** .NET · **Seen on:** .NET

The database connection was opened with an empty connection string — the configuration key wasn’t found.

## 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

1. Log builder.Configuration.GetConnectionString("Default") at startup
2. Match the name exactly
3. Set ConnectionStrings__Default in the environment for production

## Detailed fix by platform

### C#

1. 

```csharp
var cs = builder.Configuration.GetConnectionString("Default")
    ?? throw new InvalidOperationException("ConnectionStrings:Default missing");
builder.Services.AddDbContext<AppDb>(o => o.UseSqlServer(cs));
```


## How to diagnose

1. **Config** — Value at startup
2. **Environment** — ASPNETCORE_ENVIRONMENT
3. **File** — Copied to bin?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/dotnet/connectionstring-property-not-initialized)
