我们正在使用webApi作为我们最新的产品之一,它托管在IIS7.5上,用ASP.NET5 (.Net Core)编写。
在早期的web应用程序中,可以在windows-Explorer中更改web.config
中的连接字符串,而运行在IIS上的网站使用web.config
文件中的connectionstring。
对于现代WebAPI(我有ConnectionString insite appsettings.json
),这仍然可能吗?我没有在IIS或文件资源管理器中找到解决方案,并且使用环境变量不符合我们的需要。
我们需要在几个DB实例之间切换,所以非常简单的基于文件的解决方案将是非常受欢迎的。
PS:我们使用的是EntityFrameworkCore (又名EF7)数据库--首先因为它是在当前数据库之上的一个新工具。
发布于 2016-05-26 19:00:15
您可以使用其中一个配置源(包括AddJsonFile()
在包Microsoft.Extensions.Configuration.Json
中访问的JSON )读取设置文件,然后使用从中读取的值配置EF。
示例代码可能如下所示:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
有关更多信息,请参见 in ASP.NET Core docs或this answer。
https://stackoverflow.com/questions/37464680
复制相似问题