【发布时间】:2018-05-21 23:50:17
【问题描述】:
我正在从一个空的 ASP.NET Core 2 模板创建一个新网站,并遵循 Microsoft Entity Framework Tutorial 来帮助我进行设置。有一次它让你添加代码:
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
到Startup.cs 的ConfigureServices() 方法。我这样做了,但在我的项目中,Visual Studio 在Configuraiton.GetConnectionString 中的Configuration 下给了我那条小红线
我原以为我缺少 using 语句甚至是包,但 Visual Studio 2017 快速操作没有识别要使用的 using 语句,我确实安装了 Microsoft.AspNetCore.All 包,所以我应该有所有的包。
我错过了什么导致 Configuration 无法识别?
编辑:错误是:
当前上下文中不存在名称“配置”
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CollectionContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
【问题讨论】:
-
确切的错误是什么?
-
当前状态的问题尚不清楚,因为它不完整,需要太多问题才能阐明所问的内容。阅读How to Ask,然后提供minimal reproducible example,可用于重现您的问题,让我们更好地了解所要求的内容。
-
试试 Microsoft.Extensions.Configuration nuget 包。
-
根据更新,@gldraphael 的答案似乎适用于您的问题。
Configuration不是静态的。它应该是Startup类的实例变量/属性。 -
应该是,但即使在 3.0.0 中也不见了!他们编写 - 编译以查看它是否有效,当然它不会编译,所以我猜他们只是说而不做......这些教程上的糟糕 QA/QC:请参阅我在答案下的评论。
Rick Anderson应该开始练习他所教的内容...跟随非工作指南令人沮丧...
标签: c# asp.net-core entity-framework-core asp.net-core-2.0 asp.net-core-3.0