我正在摆弄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()方法的来源。
我怎样才能正确弄清楚我错过了什么?
答案 0 :(得分:2)
IConfiguration 应该在string Get(string key)
包中使用"Microsoft.Framework.Configuration.Json": "1.0.0-beta6"
方法
我不确定在beta5中。
我刚刚测试了最新版本:
"Microsoft.Framework.Configuration.Json": "1.0.0-beta8-15562"
它也已从那里删除。
根据Peter的评论,您可以以索引方式访问json属性。
示例:
public class Startup { public IConfiguration Configuration {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 :(得分:0)
当我遇到类似的错误时,我刚从框架中删除了dnxcore50
- 一些编译错误引用了dnxcore50
。
"frameworks": {
"dnx451": {
"dependencies": {
"RRStore.EF": "1.0.0-*"
}
},
"dnxcore50": { }
},