IConfiguration不包含Get()的定义(EF7,vNext)

Ast*_*aar 4 asp.net asp.net-mvc entity-framework asp.net-core

我正在摆弄EntityFramework 7和ASP.NET 5/vNext.我按照本教程.但是,当我尝试从config.json文件中获取连接字符串时,我遇到了一个问题:

'IConfiguration' does not contain a definition for 'Get' and no extension method 'Get' accepting a first argument of type 'IConfiguration' could be found (are you missing a using directive or an assembly reference?)

我不认为我错过了一个引用,但这里是project.json依赖项部分:

  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta5",
    "Microsoft.AspNet.Mvc": "6.0.0-beta5",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta5",
    "System.Net.Http": "4.0.0-beta-23019",
    "Microsoft.AspNet.WebApi": "5.2.3",
    "Microsoft.AspNet.WebUtilities": "1.0.0-beta5",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta5",
    "Microsoft.Owin.Security": "3.0.1",
    "Microsoft.AspNet.Hosting": "1.0.0-beta5",
    "Kestrel": "1.0.0-*",
    "Microsoft.AspNet.WebApi.Owin": "5.2.3",
    "Microsoft.Owin.Security.OAuth": "3.0.1",
    "Microsoft.AspNet.Mvc.Core": "6.0.0-beta5",
    "Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-beta5",
    "Microsoft.AspNet.Identity.Owin": "2.2.1",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta5",
    "EntityFramework.SqlServer": "7.0.0-beta8-15186",
    "EntityFramework.Commands": "7.0.0-beta5",
    "Microsoft.AspNet.Http.Abstractions": "1.0.0-beta8-15078",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8-15086"
  }
Run Code Online (Sandbox Code Playgroud)

以下是导致问题的代码(在Startup.cs中):

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddEntityFramework()
                    .AddSqlServer()
                    .AddDbContext<RibandelleDbContext>(options =>
                    {
                        options.UseSqlServer(Configuration.Get("Data:ConnectionString"));
                    });            
            }
Run Code Online (Sandbox Code Playgroud)

Configuration.Get("Data:ConnectionString")位返回上面的错误.我已经尽力将代码与文档样本进行比较,看起来与我完全相同.我无法弄清楚Get()方法的来源.

我怎样才能正确弄清楚我错过了什么?

Pet*_*ter 6

它似乎IConfiguration.Get()在beta5中删除了.不确定这是否是最佳选择,但您应该能够使用索引器来访问该设置.像这样的东西:

services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<RibandelleDbContext>(options =>
    {
        options.UseSqlServer(Configuration["Data:ConnectionString"]);
    });
Run Code Online (Sandbox Code Playgroud)

  • 我感觉到你的痛苦.随着代码的开发,很难跟上所有API的变化.我想这是我们使用测试版软件支付的价格 - 即使是文档也无法跟上代码的当前状态.为了回答你的问题,我通过查看github上IConfiguration.cs的源代码找到了这个解决方案. (3认同)