[英]Error when trying to create a Controller in .NET CORE Web Application
I have a .NET CORE Web application created in Visual Studio 2017. It was created as a empty template.我有一个在 Visual Studio 2017 中创建的 .NET CORE Web 应用程序。它被创建为一个空模板。
The startup.cs has the below code startup.cs 有以下代码
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: program.cs 如下所示:
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".我选择添加的控制器类型是“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确保其中有一个 Startup 类和一个 Configuration 属性
Couldn't figure out why this error is happening.无法弄清楚为什么会发生此错误。 Is it because of DI or Entity Context issue ?是因为 DI 还是实体上下文问题?
You would have to make sure that the DbContext is registered in the Startup class ConfigureServices() method as well.您还必须确保 DbContext 也在启动类ConfigureServices()方法中注册。
First Injection IConfiguration interface in Startup Class: Startup类中的第一次注入IConfiguration接口:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
then Add Below Code To ConfigureServices Method:然后添加以下代码到 ConfigureServices 方法:
services.AddDbContext<AppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
and then Add ConnectionString Address in appsettings.json :然后在appsettings.json 中添加 ConnectionString 地址:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
You must first configure the dependency injection of the DbContext.必须先配置 DbContext 的依赖注入。 This tutorial explain step by step what you need to do.本教程逐步解释您需要做什么。
Try Updating the packages in the projects using nuget package manager to the latest.尝试使用 nuget 包管理器将项目中的包更新到最新版本。
This solved the problem "I am using .Net 5.0" so this might work for you.这解决了“我正在使用 .Net 5.0”的问题,所以这可能对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.