.Net Core Cant read connection string from appsettings.json

13,342

Your ConnectionStrings section is inside the Logging section, I don't know if that's what you meant but anyway you can access the DatabaseConnection like this:

var connection = Configuration["Logging:ConnectionStrings:DatabaseConnection"];
Share:
13,342
Ekos
Author by

Ekos

Updated on August 03, 2022

Comments

  • Ekos
    Ekos over 1 year

    I'm currently creating a Web Api in .net core 2.0 and I simply cant get my connection string to work.

    I've put my connectionstring into appsettings.json and load it in startup.cs

    Appsettings.json

    {
    "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "ConnectionStrings": {
      "DatabaseConnection": "Data Source=VMDEVSLN-EOE\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"
      }
     }
    }
    

    Startup.cs

            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)
        {
            services.AddMvc();
            var connection = Configuration.GetConnectionString("DatabaseConnection");
            services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(connection));
        }
    
        // 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();
        }
    }
    

    DbContext

    public class DatabaseContext : DbContext
    {
        public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
        {
    
        }
        public DbSet<CustomerTB> CustomerTB { get; set; }
    }
    

    I believe the problem lies somewhere in my appsettings.json but I just cant find it

    • Brivvirs
      Brivvirs over 6 years
      What if you change Configuration.GetConnectionString("DatabaseConnection");to Configuration["ConnectionStrings:DatabaseConnection"]
    • Ekos
      Ekos over 6 years
      Still the same error. Doesnt solve it :(
    • Brivvirs
      Brivvirs over 6 years
      Can you set break point if the connection string is taken from appSettings?
    • Ekos
      Ekos over 6 years
      I did set a break point. It shows var connection = null
    • baruchiro
      baruchiro over 5 years
      Put a breakpoint on "Configuration" and check if the connection string is in the data of one of the providers
    • baruchiro
      baruchiro over 5 years
      Try to move appsettings.json to sln directory
  • Ekos
    Ekos over 6 years
    This didnt fix the problem aswell :(
  • Kirk Larkin
    Kirk Larkin over 6 years
    That is an awesome spot.
  • anserk
    anserk over 6 years
    Thanks, it took me a few minutes to figured it out.
  • Pankaj Kapare
    Pankaj Kapare over 6 years
    @KirkLarkin : Technically nothing wrong with "Logging:ConnectionStrings:DatabaseConnection" but ConnectionStrings should be sibling of Logging and not child of it so that you can access it using ConnectionStrings:DatabaseConnection.
  • Ekos
    Ekos over 6 years
    THANK YOU! I didnt notice that "DatabaseConnection" was a child of "Logging" instead of a sibling :D It works now
  • Neil Walker
    Neil Walker over 6 years
    I've spent hours on this then came across this, and doh I did the same thing! Thanks.
  • chitgoks
    chitgoks almost 6 years
    Man ... i got victimized by this too!
  • MC9000
    MC9000 over 4 years
    'Configuration' is a type, which is not valid in the given context. How to specify the correct context?