我有一个.NET 5 Web Api项目(VS 2015)使用实体框架6.1.3使用了一些项目引用(VS 2013)。
运行Web Api项目时,我遇到了异常"没有名为' MyEntities'的连接字符串。可以在应用程序配置文件中找到"。
这是我的配置:
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
"EntityFramework": "6.1.3",
"structuremap": "3.1.6.186",
"Microsoft.AspNet.Mvc": "6.0.0-beta6",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta6",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta5",
"System.ServiceModel.Duplex": "4.0.0-beta-23019",
"System.ServiceModel.NetTcp": "4.0.0-beta-23019",
"System.ServiceModel.Security": "4.0.0-beta-23019"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --config hosting.ini",
"ef": "EntityFramework.Commands"
},
"frameworks": {
"dnx451": {
"dependencies": {
"CuttingEdge.Conditions": "1.0.0-*",
"Quartz": "1.0.0-*",
"Project.Application": "1.0.0-*",
"Project.Finance": "1.0.0-*",
"Project.Infrastructure": "1.0.0-*",
"Project.QuartzScheduler": "1.0.0-*",
"Project.ServiceDesks": "1.0.0-*",
"Project.Utilities": "1.0.0-*",
"Project.WCFMock": "1.0.0-*"
},
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
}
}
},
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
],
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
]
}
这是我的Startup.cs:
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connectionString = Configuration.Get("Data:BrokerageSimulatorEntities:ConnectionString");
和config.json:
"Data": {
"MyEntities": {
"ConnectionString": "myConnectionString"
},
请帮助我!