1

I have a .NET CORE Web application created in Visual Studio 2017. It was created as a empty template.

The startup.cs has the below code

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IInventoryServices, InventoryServices>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseMvcWithDefaultRoute();
    }

The program.cs is like below:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}

I tried to create a Controller. The type of the controller I selected to add was "MVC Controller with views, using Entity Framework". When trying to create, in the window i have specified the model class, and ticked for "Generate views", "Reference script libraries" and "Use layout page" which by the way are ticked by default. The text-box to specify the Layout page is left blank.

When trying to create the controller I get the below error:

There was an error running the selected code generator: Scaffolding failed to edit Startup class to register the new Context using Dependency injection. Make sure there is a Startup class and a Configuration property in it

Couldn't figure out why this error is happening. Is it because of DI or Entity Context issue ?

1

3 Answers 3

1

You would have to make sure that the DbContext is registered in the Startup class ConfigureServices() method as well.

First Injection IConfiguration interface in Startup Class:

public IConfiguration Configuration { get; }

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

then Add Below Code To ConfigureServices Method:

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

and then Add ConnectionString Address in appsettings.json :

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
  } 
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the reply. Just one clarification. I didn't understand the IConfiguration injection. Can i know whats the purpose of it ?
IConfiguration interface for reading config from appsettings.json according to the key. Could you overcome the problem?
I am a newbie to Core. I dont have a appsettings,json in my project. There is a launchSettings.json in the properties section. How should I add the ConnectionString. Please advise
@sms101 For learning about concpet asp.net core please see this link: stackoverflow.com/a/59701684/5576498
|
0

You must first configure the dependency injection of the DbContext. This tutorial explain step by step what you need to do.

ASP.NET With Entity Framework

Comments

0

Try Updating the packages in the projects using nuget package manager to the latest.

This solved the problem "I am using .Net 5.0" so this might work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.