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 had the following error in package manager console when Add-Migration.
This is my startup:
namespace WebApi
{
public class Startup
{
private string _connectionString = null;
public Startup(IConfiguration configuration)
{
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)
{
_connectionString = Configuration["secretConnectionString"];
services.AddMvc();
services.AddEntityFrameworkNpgsql().AddDbContext<ApiContext>
(Options => Options.UseNpgsql(_connectionString));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}
appsettings.json:
"connectionString": {
"_connectionString": " User Id=Pdev; Password=abcd;Server=localhost;Port=5432;Database=Advantage.Api.Dev;Integrated Security=true;Pooling=true;"
},
Instead of
_connectionString = Configuration["secretConnectionString"];
try using
_connectionString = Configuration.GetValue<string>(
"connectionString:_connectionString")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.