New .Net Core 2 Site does not reconize Configuration.GetConnectionString
Asked Answered
T

5

18

I am creating a new web site from an empty ASP.NET Core 2 template and following the Microsoft Entity Framework Tutorial to help me get setup. At one point it has you add the code:

services.AddDbContext<SchoolContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

To the ConfigureServices() method of Startup.cs. I did this but in my project Visual Studio give me that little red line under Configuration in the Configuraiton.GetConnectionString

I had thought I was missing a using statement or even a package but the Visual Studio 2017 quick actions don't identify a using statement to use and I do have the Microsoft.AspNetCore.All package installed so I should have all the packages.

What am I missing that is making the Configuration not recognized?

Edit: The error is:

The name 'Configuration' does not exist in the current context

public void ConfigureServices(IServiceCollection services)
{
     services.AddDbContext<CollectionContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
     services.AddMvc();
}
Talishatalisman answered 7/12, 2017 at 19:27 Comment(5)
what's the exact error?Amena
The question in its current state is unclear as it is incomplete and would require too many questions to clarify what is being asked. Read How to Ask and then provide a minimal reproducible example that can be used to reproduce your problem, allowing us to better understand what is being asked.Thatcher
Try the Microsoft.Extensions.Configuration nuget package.Commonalty
Based on the update, @gldraphael 's answer seems to apply to your problem. Configuration is not static. It should be an instance variable/property of the Startup class.Thatcher
Should be, but even in 3.0.0 it is missing! They write - compile to see if it works, and of course it does not compile, so I guess they just say and do not do... Lousy QA/QC on these tutorials: See my comment under the answer. Rick Anderson should start practicing what he teaches... it is frustrating to follow non-working guides...Battery
A
42

You need to get the IConfiguration object via DI.
Add a IConfiguration argument to your Startup's constructor, and assign it to a Configuration property:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

I'm surprised how you don't have it though, because it's part of the template.

Amena answered 7/12, 2017 at 19:51 Comment(10)
Thanks, I would bet that is part of the normal MVC template but that template adds other things I did not want so I went with the empty template where I had to add the servives.AddMVC() my self so this is probability something I do need to add. I will test on lunch break tomorrow when I work on this project again.Talishatalisman
This worked, I checked the normal MVC project template and it is part of that template but it is not part of an empty template.Talishatalisman
Also seems to be missing in Web API project, even though Web API is delivered via MVC in asp.net core 2, as I understand it. Looks like using Microsoft.Extensions.Configuration; is what I want...Tram
@Andrew I have the feeling you're doing something wrong... You shouldn't need to inject IConfiguration into your DbContext... feel free to open a new question and someone will help you.Amena
This tutorial never works out of box. Now, for 3.0.0 preview (at least for VS for mac: docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/… ) the Startup.cs is missing declaration for Configuration, e.g. something like private IConfiguration Configuration; ( FYI: The lines 11 - 28 of Startup.cs: i.sstatic.net/cL8X8.png ) ... also the Add NuGet packages and EF tools section on the tutorial page follows their use in the Add a database context class section ... no/poor QA/QC on these tutorials...Battery
If you need access to Configuration outside of the Startup class, you may want to declare it as a public property instead of a private field, either as a read/write: public IConfiguration Configuration { get; set; } or read a only: public IConfiguration Configuration { get; }Battery
Cool solution! Thanks :)Chemise
If you're to .NET Core, consider reading my blog post on using IConfiguration. Wrote it based on the other comments here.Amena
Your are right @gldraphael . I think they are having issue since the have created an empty project like me. Empty Project doesnot configure all. Thanks :)Elke
This solved issue that's held me for a while. Thanks. I built project starting with ASP.NET Core 5. I wonder why my Startup.cs didn't include it.Whereinto
M
4

1# install the NuGet package: Microsoft.Extensions.Configuration
2# add: using Microsoft.Extensions.Configuration;
3# Note that i have added this line in the code: public IConfiguration Configuration { get; }

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers(); 
            });
        }
    }
Mckenziemckeon answered 31/1, 2020 at 13:31 Comment(0)
H
2

Sometimes you add the wrong namespace like AutoMapper.Configuration in place of the correct one Microsoft.Extensions.Configuration.

Heedless answered 17/6, 2020 at 5:27 Comment(0)
I
-1

Do not forget add public IConfiguration Configuration { get; }

Incubator answered 28/5, 2020 at 10:27 Comment(0)
U
-1

if you are using asp .net core mvc 5.0 then in program.cs just write like

builder.Configuration.GetConnectionString("DefaultConnection")));
Utham answered 22/1, 2022 at 18:31 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Mitzi

© 2022 - 2024 — McMap. All rights reserved.