Value cannot be null. Parameter name: connectionString appsettings.json in starter

前端 未结 14 1849
南旧
南旧 2020-12-16 10:07

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: connection

相关标签:
14条回答
  • 2020-12-16 10:40

    For Postgre Db, Below worked for me

        private readonly IConfiguration _config;
    
        string connectionString;
        public DataBaseContext(IConfiguration config)
        {
            _config = config;
            connectionString= _config.GetValue<string>("ConnectionStrings:sampleConnection");
        }
    

    Config file:

    "ConnectionStrings": { "sampleConnection":".." }

    Start up file: services.AddScoped();

    0 讨论(0)
  • 2020-12-16 10:43

    First of all, the

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

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

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

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

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

    var config2 = Configuration.GetConnectionString("DefaultConnection");
    
    0 讨论(0)
  • 2020-12-16 10:45

    Another mistake in my case was that I was using ConnectionString, instead ConnectionStrings (Note last 's')

    0 讨论(0)
  • I was missing the letter 's' after the ConnectionString property name in the appsettings.json when using Configuration.GetConnectionString("name")

    If you want to copy

    "ConnectionStrings ": {
      "SpyStore": "Server=(localdb)\\mssqllocaldb;Database=SpyStore;Trusted_Connection=True;MultipleActiveResultSets=true;"
    }
    

    The method wording GetConnectionString confused me, I hovered over it and oh be hold it was looking for ConnectionStrings property name instead of ConnectionString

    0 讨论(0)
  • 2020-12-16 10:51

    You need to change your appsetting.jsonto:

        {
      "Data": {
        "ConnectionStrings": {
          "DefaultConnection": "Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true"
    
        },
        "Logging": {
          "IncludeScopes": false,
          "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
          }
        }
      }
    }
    

    And now will be working:

      var connStr = Configuration.GetConnectionString("DefaultConnection");
    
    0 讨论(0)
  • 2020-12-16 10:52

    in asp.net core you must add IConfiguration to startUp contractor method and assignee this Parameter to a property inherited from IConfiguration inside class

     public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            this.Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
    
    0 讨论(0)
提交回复
热议问题