问题
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
}
回答1:
First of all, the
"Data": {
"ConnectionStrings": {
"DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"},
}
Is slightly different from the structure you get when you add a "Asp.NET Configuration File" in Visual Studio. When you do that you get
"ConnectionStrings": {
"DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"},
without the "Data" JavaScript Object. So that's why the extension method isn't working. It expects this structure. You can use this structure (the one with "Data") anyway and get your connection string like so:
var connectionString = Configuration["Data:ConnectionStrings:DefaultConnection"];
Notice that you are navigating through the JavaScript object tree using :
instead of .
. That's due to some cross-platform issues with using the .
.
If you edit out the "Data":{} you can do this :
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Now the extension method will work. Underneath the Microsoft extensions it is the same thing as the code above.
var config2 = Configuration.GetConnectionString("DefaultConnection");
回答2:
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
回答3:
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"];
回答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.
回答5:
You need to change your appsetting.json
to:
{
"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");
回答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")));
回答7:
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")));
回答8:
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.
回答9:
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;"
回答10:
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; }
来源:https://stackoverflow.com/questions/40874640/value-cannot-be-null-parameter-name-connectionstring-appsettings-json-in-start