【问题标题】:Error when get connection string: ArgumentNullException: Value cannot be null. Parameter name: connectionString获取连接字符串时出错:ArgumentNullException:值不能为空。参数名称:连接字符串
【发布时间】:2020-01-29 02:02:54
【问题描述】:

我正在使用 ASP.NET Core 2.0。下面是我的代码。

启动:

namespace foo
{
    public class Startup
    {
        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)
        {
            // Add framework services.
            services
                .AddMvc()
                .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
            services.AddDbContext<fooContext>(options => options.UseSqlServer(Configuration.GetConnectionString("UserDatabase")));
        }

        // 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.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "ConnectionStrings": {
      "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"
    }
  }
}

如何解决?

【问题讨论】:

  • 尝试将 ConnectionString 放在 JSON 文件的顶部
  • 行得通,你很专业
  • 是的@Sajeetharan 是对的。您在 Logging 中拥有 ConnectionStreeings。在那里它不会被发现。将 ConnectionStrings 移到顶层的 logging 之外
  • 谢谢,错误类型我抓了很多次。我会记住的。

标签: c# asp.net-core entity-framework-core asp.net-core-2.1


【解决方案1】:

问题是您的 ConnectionStrings 对象一直是 Logging 对象的属性。写你的appsettings.json如下:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
  },
  "ConnectionStrings": {
      "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"
  }
}

【讨论】:

    【解决方案2】:

    正如评论中提到的,尝试将您的连接字符串移动到顶部(建议)修复是获取密钥 *"ConnectionStrings" *outside of logging 密钥

    appsettings.json

    {
      "ConnectionStrings": {
          "UserDatabase": "Server=DESKTOP-FSES7UK;Database=xxx;User Id=sa; Password=xxxxxxx;Trusted_Connection=True;"
      },
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Debug",
          "System": "Information",
          "Microsoft": "Information"
      }
    }
    

    【讨论】:

    • try to move your connection string to the top -- 这不是问题!您可以将其放在appsetings.json 内的任何位置。
    • 这不是真正的问题,但我建议将它放在顶部
    • or outside of the logging key-- 这意味着这也不是强制性的!这是强制性的。不能把OR和上一句放在一起!
    • 哈哈!感谢您的纠正。我希望 OP 得到答案 :) 所以没关系。
    • @Sajeetharan 欢迎!但是在您多次修改答案之前发布了正确答案?
    猜你喜欢
    • 2019-11-06
    • 2021-01-29
    • 2018-02-11
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多