.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"];
Author by
Ekos
Updated on August 03, 2022Comments
-
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 over 6 yearsWhat if you change
Configuration.GetConnectionString("DatabaseConnection");
toConfiguration["ConnectionStrings:DatabaseConnection"]
-
Ekos over 6 yearsStill the same error. Doesnt solve it :(
-
Brivvirs over 6 yearsCan you set break point if the connection string is taken from appSettings?
-
Ekos over 6 yearsI did set a break point. It shows var connection = null
-
baruchiro over 5 yearsPut a breakpoint on "Configuration" and check if the connection string is in the data of one of the providers
-
baruchiro over 5 yearsTry to move appsettings.json to sln directory
-
-
Ekos over 6 yearsThis didnt fix the problem aswell :(
-
Kirk Larkin over 6 yearsThat is an awesome spot.
-
anserk over 6 yearsThanks, it took me a few minutes to figured it out.
-
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 over 6 yearsTHANK YOU! I didnt notice that "DatabaseConnection" was a child of "Logging" instead of a sibling :D It works now
-
Neil Walker over 6 yearsI've spent hours on this then came across this, and doh I did the same thing! Thanks.
-
chitgoks almost 6 yearsMan ... i got victimized by this too!
-
MC9000 over 4 years'Configuration' is a type, which is not valid in the given context. How to specify the correct context?