'Value cannot be null. Parameter name: connectionString appsettings.json in starter
I am trying to write my connection string in my appsettings.json file and bring it into my startup file but I keep getting a Value cannot be null. Parameter name: connectionString. I have been using various examples but can't seem to see this new setup with ASP.NET 1.0 Core startup class.
Appsetting.json file:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
Method attempting Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
var connStr = Configuration.GetConnectionString("DefaultConnection");
System.Console.WriteLine(connStr);
services.AddDbContext<DbContext>(options => options.UseSqlServer(connStr)); //error right here -- Null value
}
Solution 1:[1]
I was missing the letter 's' after the ConnectionString property name in the appsettings.json when using Configuration.GetConnectionString("name")
If you want to copy
"ConnectionStrings ": {
"SpyStore": "Server=(localdb)\\mssqllocaldb;Database=SpyStore;Trusted_Connection=True;MultipleActiveResultSets=true;"
}
The method wording GetConnectionString confused me, I hovered over it and oh be hold it was looking for ConnectionStrings property name instead of ConnectionString
Solution 2:[2]
DefaultConnection is the inner object in the json structure and it is the child of Data object.
So if you want to be exact with your config file you can use
var connStr = Configuration.GetSection("Data")
.GetSection("DefaultConnection")["ConnectionString"];
Solution 3:[3]
I got this because I had a connection string with a \ in it, which needed escaping to be a \\. So my localdb connection string was causing a load error like this:
"DefaultConnection": "Server=(localdb)\myinstance;Integrated Security=true;Initial Catlog=my-localdb;"
and was fixed by making it:
"DefaultConnection": "Server=(localdb)\\myinstance;Integrated Security=true;Initial Catlog=my-localdb;"
Solution 4:[4]
I had got similar error.My "appsettings.json" file was not loading because the properties of the file was Copy to Output Directory -> Do not Copy. I set that to Copy always save and rebuild.It worked.
Solution 5:[5]
in asp.net core you must add IConfiguration to startUp contractor method and assignee this Parameter to a property inherited from IConfiguration inside class
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public IConfiguration Configuration { get; }
Solution 6:[6]
this is was the message that appeared me
Value cannot be null. Parameter name: connectionString
I fix it changed in the startup these lines
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
Configuration["Data:BookStoreContext:ConnectionString"]));
To
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("BookStoreContext")));
Solution 7:[7]
I experienced this problem using asp.net core 3.0. The workaround fix for me is:
Instead of:
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Use this:
var connectionString = Configuration["ConnectionStrings::DefaultConnection"];
The emphasis here is the double colon in the connection configuration.
Solution 8:[8]
Another mistake in my case was that I was using ConnectionString, instead ConnectionStrings (Note last 's')
Solution 9:[9]
Maybe you can try this:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer("DefaultConnection")
);
Then set your appsettings.json do something like this:
"ConnectionStrings:": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=HOME1_DATABASE;Trusted_Connection=True;MultipleActiveResultSets=true"
},
After that, when a I add migration via the console, it succeeds.
Solution 10:[10]
While using Postgres SQL the appsettings.Development.json must contain
"ConnectionStrings": {
"DefaultConnection": "User ID=user;Password=xxxx;Host=127.0.0.1;Port=5432;Database=abc;Pooling=true"
}
Content in the Startup.cs file would be like
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>
(p => p.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
}
Solution 11:[11]
Solution 12:[12]
The correct way to add connection strings into your appsetting.json file is the following:
"ConnectionStrings":{
"DefaultConnection":"Server=localdb)\\MSSQLLocalDB;Datbase=MyShop;Trusted_Connection=true"
}
Solution 13:[13]
I get it,it was because the name of connection string that in app settings was difference from that in the configuration that in Startup !!!
The right code of connection String Configuration in startup
// replace NAMEHERE with the name of the class that inherits from DbContext
services.AddDbContextPool <NAMEHERE> (opts => opts.UseSqlServer(Configuration.GetConnectionString("here put the same Connection string name that in appsettings")));
Solution 14:[14]
I fought with the null connection string issue far too long, only to find I was doing this:
builder.Services.AddDbContext<MlDataContext>(provider => new MlDataContext(config.GetConnectionString("ConnectionStrings:SqlConnectionString")));
I was using both the ConnectionStrings: prefix AND GetConnectionString. When I removed the prefix it worked:
builder.Services.AddDbContext<MlDataContext>(options => options.UseSqlServer(config.GetConnectionString("SqlConnectionString")));
Solution 15:[15]
For Postgre Db, Below worked for me
private readonly IConfiguration _config;
string connectionString;
public DataBaseContext(IConfiguration config)
{
_config = config;
connectionString= _config.GetValue<string>("ConnectionStrings:sampleConnection");
}
Config file:
"ConnectionStrings": { "sampleConnection":".." }
Start up file: services.AddScoped();
Solution 16:[16]
In my case I had two IConfiguration definitions in my Startup class. Not helpful :)
Solution 17:[17]
I had this error on the day when usin Asp.Net Core 5. I tried all above solutions with no satisfaction. Then I deleted the "ConnectionStrings" section the appsettings.json file and launched a migration. After the error, I rewrote the section and launched again the migration. All worked fine.
Solution 18:[18]
Please make sure you have all you connection strings defined in the app.json. My problem was I had two different projects that added a dbContext in their services config
- The identity project which made a reference to the identityDb
- The persistence project that all my data access logic
I solved the issue by having both connection strings defined in the app.json
Solution 19:[19]
try it, it will work for you on version 3.0.0.
public class Startup { private readonly IConfiguration configuration; public Startup(IConfiguration configuration) { this.Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MyPortfolio"));
});
}
appsettings.json
{
"Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "MyPortfolio": "Data Source=(localdbd)\MSSQLLocalDB; initail catalog=MyPortfolioDB; Integrated Security=true" } }
Solution 20:[20]
I had the same problem and this fixed it for me:
connStr = Configuration.GetConnectionString("DefaultConnection");
change to:
connStr = Configuration["DefaultConnection:ConnectionString"]
Solution 21:[21]
You need to change your appsetting.jsonto:
{
"Data": {
"ConnectionStrings": {
"DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
And now will be working:
var connStr = Configuration.GetConnectionString("DefaultConnection");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow


