Error when trying to create a Controller in .NET CORE Web Application

南楼画角 提交于 2020-11-30 00:29:29

问题


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:


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




回答2:


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"
  } 
}



回答3:


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.



来源:https://stackoverflow.com/questions/59651008/error-when-trying-to-create-a-controller-in-net-core-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!