值不能为空.参数名称:starter中的connectionString appsettings.json [英] Value cannot be null. Parameter name: connectionString appsettings.json in starter

查看:187
本文介绍了值不能为空.参数名称:starter中的connectionString appsettings.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的appsettings.json文件中写入连接字符串,并将其带入我的启动文件中,但我一直在获取Value不能为null. 参数名称:connectionString.我一直在使用各种示例,但似乎看不到带有ASP.NET 1.0 Core启动类的新设置.

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文件:

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

方法尝试 Startup.cs

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
}

推荐答案

首先,

 "Data": {
"ConnectionStrings": {
  "DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"},
}

与在Visual Studio中添加"Asp.NET配置文件"时所获得的结构略有不同.当您这样做时,您会得到

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"},

没有数据" JavaScript对象.这就是扩展方法不起作用的原因.它期望这种结构.无论如何,您都可以使用这种结构(带有"Data"的结构),并像这样获得连接字符串:

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"];

请注意,您正在使用:而不是.在JavaScript对象树中导航.这是由于使用.时存在一些跨平台问题.

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"];

现在,扩展方法将起作用.在Microsoft扩展程序的下面与上面的代码相同.

Now the extension method will work. Underneath the Microsoft extensions it is the same thing as the code above.

var config2 = Configuration.GetConnectionString("DefaultConnection");

这篇关于值不能为空.参数名称:starter中的connectionString appsettings.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆