Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm fiddling with EntityFramework 7 and ASP.NET 5/vNext. I follow this tutorial. However I run into an issue 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 that I'm missing a reference, but here is the project.json dependencies 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 issue (in Startup.cs):

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

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

The Configuration.Get("Data:ConnectionString") bit returns the error above. I've done my best to compare the code to the documentation samples, and it seems fairly identical to me. I can't figure out where the Get() method comes from.

How can I properly figure out what I'm missing?

like image 215
Astaar Avatar asked Jan 07 '23 14:01

Astaar


2 Answers

It looks like IConfiguration.Get() was removed in beta5. Not sure if this is the best option, but you should be able to use the indexer to access the setting. Something like this:

services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<RibandelleDbContext>(options =>
    {
        options.UseSqlServer(Configuration["Data:ConnectionString"]);
    });
like image 98
Peter Avatar answered Jan 24 '23 07:01

Peter


IConfiguration should have the method string Get(string key) in "Microsoft.Framework.Configuration.Json": "1.0.0-beta6" package

I'm not sure in beta5 though.

I have just tested with the latest version:

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

and it has been removed from there as well.

As per Peter's comment you can access the json properties in an indexed way.

Example:

public class Startup { public IConfiguration Configuration { get; private set; }

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!");
    });
}

}

assuming that you have a config.json file with the Connection String under Data like below:

{
  "Data": {
    "ConnectionString": "Server=.;Database=Your_Database_Name;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}
like image 40
diegosasw Avatar answered Jan 24 '23 08:01

diegosasw