【发布时间】:2018-02-11 13:36:27
【问题描述】:
我一直按照https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db 的步骤创建项目,当我到达为我的一个实体搭建控制器的最后一步时,我收到错误“没有为此对象定义无参数构造函数”。我已经查看了来自 stackoverflow 的链接以解决完全相同的问题 ASP.NET MVC: No parameterless constructor defined for this object,但我有一个无参数构造函数,如下面的代码所示。
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
var connection = "connection string here";
services.AddDbContext<SchoolManagementContext>(options => options.UseSqlServer(connection));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
}
program.cs
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
【问题讨论】:
标签: c# entity-framework asp.net-core asp.net-core-mvc