asp.net - 值不能为空。参数名称: connectionString when i run add-migration

标签 asp.net angular compiler-errors migration

我试图在我的appsettings.json文件中写入连接字符串,并将其带入我的启动文件中,但我一直在获取Value不能为null。参数名称:connectionString。
添加迁移时,程序包管理器控制台中出现以下错误。
这是我的创业公司:


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")

关于asp.net - 值不能为空。参数名称: connectionString when i run add-migration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62996051/

相关文章:

c - 我的 switch 语句有什么问题?

asp.net - Web API 无法解析 System.Web.Http 中的符号 Http,缺少什么?

Angular 2 : Error while using a text filter pipe to filter an array

angular - 在 Angular 4 中,如何在重定向路由中保留查询参数?

forms - 提交表单-由于过程漫长而卡住

vba - 尝试从Visual Basic循环搜索

java - 主管策略 : Compile error - Resume/Restart/Stop/Escalate incompatible with Directive

c# - 防止回发 vb 和 javascript

asp.net - 使用 Web 服务检索数据还是直接从 .net 中的数据库检索数据更好

c# - 在 GridView 中为复选框列生成新列