[英]Value cannot be null. Parameter name: connectionString when i run add-migration
我正在尝试在我的 appsettings.json 文件中写入我的连接字符串并将其带入我的启动文件,但我不断得到一个值不能是 null。 参数名称:连接字符串。 添加迁移时,我在 package 管理器控制台中出现以下错误。
这是我的启动:
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;"
},
代替
_connectionString = Configuration["secretConnectionString"];
尝试使用
_connectionString = Configuration.GetValue<string>(
"connectionString:_connectionString")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.