【问题标题】:IConfiguration does not contain a definition for Get() (EF7, vNext)IConfiguration 不包含 Get() 的定义(EF7、vNext)
【发布时间】:2015-09-10 15:18:55
【问题描述】:

我在摆弄 EntityFramework 7 和 ASP.NET 5/vNext。我关注this tutorial。但是,当我尝试从 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"
  }

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

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

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

Configuration.Get("Data:ConnectionString") 位返回上述错误。我已尽力将代码与文档示例进行比较,这对我来说似乎非常相似。我无法弄清楚 Get() 方法的来源。

我怎样才能正确地找出我错过了什么?

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework asp.net-core


    【解决方案1】:

    看起来IConfiguration.Get() 在 beta5 中已被删除。不确定这是否是最佳选择,但您应该能够使用索引器访问设置。像这样的:

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

    【讨论】:

    • 啊,很好!我不知道我们可以这样使用它。以供将来参考,我怎么能自己弄清楚呢?
    • 我感受到你的痛苦。在开发代码时跟上所有 API 更改是很困难的。我想这就是我们为使用测试版软件付出的代价——即使是文档也无法跟上代码的当前状态。为了回答您的问题,我通过查看 github 上 IConfiguration.cs 的源代码找到了此解决方案。
    【解决方案2】:

    IConfiguration应该在"Microsoft.Framework.Configuration.Json": "1.0.0-beta6"包中有string Get(string key)方法

    不过我不确定 beta5 中的情况。

    我刚刚测试了最新版本:

    "Microsoft.Framework.Configuration.Json": "1.0.0-beta8-15562"
    

    它也已从那里删除。

    根据 Peter 的评论,您可以以索引方式访问 json 属性。

    例子:

    公共类启动 { 公共 IConfiguration 配置 { 获取;私人套装; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                            .AddJsonFile("config.json")
                            .AddEnvironmentVariables();
        Configuration = configurationBuilder.Build();
    
        string test = Configuration["Data:ConnectionString"]; //this reads the property
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddMvc();
    
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<RibandelleDbContext>(options =>
            {
                options.UseSqlServer(Configuration["Data:ConnectionString"]);
            });
    }
    
    public void Configure(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
    

    }

    假设您有一个config.json 文件,其数据下的连接字符串如下所示:

    {
      "Data": {
        "ConnectionString": "Server=.;Database=Your_Database_Name;Trusted_Connection=True;MultipleActiveResultSets=true"
      }
    }
    

    【讨论】:

      【解决方案3】:

      不确定这是否准确,但如果您分析该文章的sample application,您可以看到该文件需要哪些 using 语句。

      using ContosoBooks.Models;
      using Microsoft.AspNet.Builder;
      using Microsoft.AspNet.Diagnostics;
      using Microsoft.AspNet.Hosting;
      using Microsoft.Data.Entity;
      using Microsoft.Framework.Configuration;
      using Microsoft.Framework.DependencyInjection;
      using Microsoft.Framework.Logging;
      using Microsoft.Framework.Runtime;
      

      如果这不能解决您的问题,也许分析样本会。

      【讨论】:

      • 我的代码具有相同的 using 语句(当然 ContosoBooks.Models 除外),抱歉没有在问题描述中发布。
      【解决方案4】:

      当我遇到类似错误时,我刚刚从框架中删除了 dnxcore50 - 引用了 dnxcore50 的一些编译错误。

      "frameworks": {
        "dnx451": {
          "dependencies": {
            "RRStore.EF": "1.0.0-*"
          }
        },
        "dnxcore50": { }
      },
      

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 2019-07-13
        • 2021-08-03
        • 2020-08-27
        • 2019-06-20
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多