C# 无法创建类型为';ApplicationDbContext';。添加';IDesignTimeDbContextFactory<;ApplicationDbContext>';对该项目的支持
我正在尝试使用asp.net Core 2.1创建一个Angular 8项目。 每当我尝试使用命令添加迁移时 cmd命令:dotnet ef migrations add init--project../Lgn.DAL 终端抛出错误: 无法创建“ApplicationDbContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或参见https://go.microsoft.com/fwlink/?linkid=851728 用于设计时支持的其他模式 Startup.cs `` 公营创业 { 公共启动(IConfiguration配置) { 配置=配置; }C# 无法创建类型为';ApplicationDbContext';。添加';IDesignTimeDbContextFactory<;ApplicationDbContext>';对该项目的支持,c#,entity-framework,asp.net-core,angular8,C#,Entity Framework,Asp.net Core,Angular8,我正在尝试使用asp.net Core 2.1创建一个Angular 8项目。 每当我尝试使用命令添加迁移时 cmd命令:dotnet ef migrations add init--project../Lgn.DAL 终端抛出错误: 无法创建“ApplicationDbContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或参见https://go.microsoft.com/fwlink/?linkid=851728 用于设计时支持的其他
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlite(Configuration.GetConnectionString(“DefaultConnection”));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
}
app.UseCors(builder=>
{
建筑商。来源(“http://localhost:4200");
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
app.UseHttpsRedirection();
app.UseMvc();
}
}``
看看有类似问题的人。你的依赖注入设置都好吗?(名单上的第二名)
以下是需要考虑的事项:
出现该错误是因为要生成迁移,您需要:
- 具有默认构造函数(即无参数)的DbContext (建造商)
- 能够从ApplicationServices获取DbContext (即依赖项注入)
- 返回 正确配置的DbContext
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(builder =>
{
builder.WithOrigins("http://localhost:4200");
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
app.UseHttpsRedirection();
app.UseMvc();
}
}``