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();
}
Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
  • what's the exact error? – galdin Dec 07 '17 at 19:31
  • 2
    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 [ask] and then provide a [mcve] that can be used to reproduce your problem, allowing us to better understand what is being asked. – Nkosi Dec 07 '17 at 19:36
  • Try the Microsoft.Extensions.Configuration nuget package. – Dylan Nicholson Dec 07 '17 at 19:37
  • 1
    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. – Nkosi Dec 07 '17 at 21:09
  • 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... – Lucifer Morningstar Aug 23 '19 at 20:42

5 Answers5

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.

galdin
  • 12,411
  • 7
  • 56
  • 71
  • 2
    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. – Matthew Verstraete Dec 07 '17 at 21:11
  • 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. – Matthew Verstraete Dec 12 '17 at 19:06
  • 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... – ruffin Feb 09 '18 at 01:04
  • @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. – galdin Apr 22 '19 at 18:10
  • 1
    This tutorial never works out of box. Now, for 3.0.0 preview (at least for VS for mac: https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-3.0&tabs=visual-studio-mac ) the ```Startup.cs``` is missing declaration for Configuration, e.g. something like ```private IConfiguration Configuration;``` ( FYI: The lines 11 - 28 of Startup.cs: https://i.stack.imgur.com/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... – Lucifer Morningstar Aug 23 '19 at 20:35
  • 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; }` – Lucifer Morningstar Aug 28 '19 at 22:16
  • Cool solution! Thanks :) – Rohan Rao Dec 25 '19 at 12:12
  • If you're to .NET Core, consider reading my [blog post](https://galdin.dev/blog/you-dont-need-iconfiguration-outside-startup/) on using `IConfiguration`. Wrote it based on the other comments here. – galdin Jan 27 '20 at 03:42
  • 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 :) – anil shrestha Aug 03 '20 at 18:54
  • 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. – Bob Koury Jan 28 '21 at 18:45
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(); 
            });
        }
    }
Stephan
  • 41
  • 1
2

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

NHARI Med
  • 151
  • 1
  • 7
-1

Do not forget add public IConfiguration Configuration { get; }

-1

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

builder.Configuration.GetConnectionString("DefaultConnection")));
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 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](/help/how-to-answer). – Community Jan 23 '22 at 03:21