IConfiguration does not contain a definition for Get () (EF7, vNext)

I was messing around with EntityFramework 7 and ASP.NET 5 / vNext. I follow this tutorial . However, I ran into a problem when I try to get the connection string from the config.json file:

'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?)

I don’t think I am missing the link, but here is the project.json dependency section:

  "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"
  }

Here is the code causing the problem (in Startup.cs):

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

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

The bit Configuration.Get("Data:ConnectionString")returns the error above. I did my best to compare the code with the sample documentation, and it seems to me completely identical. I cannot figure out where the Get () method comes from.

How can I correctly understand what I am missing?

+4
source share
4

, IConfiguration.Get() 5. , , . - :

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

IConfiguration string Get(string key) "Microsoft.Framework.Configuration.Json": "1.0.0-beta6"

5.

:

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

.

json .

:

Startup {   public IConfiguration {get; ; }

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"
  }
}
+1

Not sure if this is the case, but if you analyze the sample application sample for this article, you will see that using the statements you need for this file.

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;

If this does not solve your problem, a sample analysis may be performed.

0
source

When I received such errors, I simply deleted dnxcore50from the frameworks - some of the referenced compilation errors dnxcore50.

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

Source: https://habr.com/ru/post/1606817/


All Articles