Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration does not exist in current context Core 2.0 #1

Open
sipi41 opened this issue Feb 11, 2018 · 3 comments
Open

Configuration does not exist in current context Core 2.0 #1

sipi41 opened this issue Feb 11, 2018 · 3 comments

Comments

@sipi41
Copy link

sipi41 commented Feb 11, 2018

In the first example of the book, it is instructed to do the following:

public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<BlogContext>(options => options.UseSqlServer(Configuration.GetConnectionString(" DefaultConnection"))); services.AddMvc(); }

But when I want to use the part of Configuration.GetConnectionString, I get a red line under saying: "The name "Configuration does not exist in current context"

Please help! (see attached file below)

error

@sipi41
Copy link
Author

sipi41 commented Feb 12, 2018

As nobody took some time to answer, here's the answer:

Configuration IS a variable we should create into the "Startup Class", this is what I did according to other book called "Pro ASP.NET Core MVC 2"

private IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
=> this.Configuration = configuration;

Basically, we create a private variable of type IConfiguration, then we create a constructor for the Startup class with injection of an IConfiguration object, then pass the created object to the private IConfiguration variable we just created.

Why the book does not explain this?

@sipi41 sipi41 changed the title Am I doing something wrong? Configuration does not exist in current context Core 2.0 Feb 12, 2018
@ghost
Copy link

ghost commented Nov 5, 2019

I've only had this issue arise (adding the DI of an IConfiguration object into the Startup class) when starting from an Empty project with no scaffolding (API or MVC Core 2.x).

Thank you sipi41 for coming back and answering this.

@ronnelltyars
Copy link

Thank you for answering this!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants