[英]Error when configuring services - Adding Entity Framework Core to ASP.NET Core
I have created an ASP.NET Core web application.我创建了一个 ASP.NET Core Web 应用程序。 I am trying to add Entity Framework Core to my solution.
我正在尝试将 Entity Framework Core 添加到我的解决方案中。
I have installed the below nugets to the project.我已将以下 nugets 安装到项目中。
There is a DbSet defined in the Context.在上下文中定义了一个 DbSet。
In the startup class when i try to add the Db Context, I get the following error in the for "Configuration".在启动类中,当我尝试添加 Db 上下文时,在“配置”中出现以下错误。
The Name Configuration does not exist in the current context.
当前上下文中不存在名称配置。
My code is:我的代码是:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
I am following this article which doesn't say much about the configuration我正在关注这篇文章,它对配置没有太多说明
The code sample is incomplete.代码示例不完整。 The Configuration actually is a property of the Startup class.
Configuration 实际上是 Startup 类的一个属性。 It is populated via constructor dependency injection.
它是通过构造函数依赖注入填充的。
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
//...
}
As mentioned on the last step of the article : the full code is available on https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/ef-mvc/intro/samples/cu-final如文章最后一步所述:完整代码可在https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/ef-mvc/intro/samples/cu-final 上获得
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.