web.config中的ASP.NET 5/EF7连接字符串? [英] ASP.NET 5/EF7 connection string in web.config?
问题描述
在ASP.Net 4.5中,我可以将连接字符串放在web.config
中以利用web.config transformations
的优势,这样我就可以在本地使用开发数据库,然后在发布时将其指向生产数据库.我现在正在使用ASP.Net 5和EF 7,它们似乎使用config.json
文件存储连接字符串而不是web.config
.使用这种新的文件存储方式,我无法弄清过去如何做web.config transformations
之类的事情.如何设置config.json
来执行此操作或对其进行配置,以便可以在web.config中进行操作,并让EF在此处查找字符串?
In ASP.Net 4.5 I could put my connection string in the web.config
as to take advantage of web.config transformations
so I could work locally with the development DB and then when I publish it would point to the production DB. I am now working with ASP.Net 5 and EF 7 which seems to use the config.json
file to store connection strings instead of the web.config
. With this new way of storing files I can not figure out how to do something like the web.config transformations
from the past. How can I either setup config.json
to do this OR configure it so I can do it in the web.config and have EF look there for the strings?
推荐答案
web.config
转换语法基于XML数据格式.新配置包含一些JSON格式的文件,并且可以非常轻松地实现暂存方案.
The web.config
transformation syntax is oriented on XML format of data. The new configurations consist of some files in JSON format and one can implement staging scenarios very easy.
首先,ASP.NET支持允许通过使用ASPNET_ENV
环境变量或通过在launchSettings.json
文件(请参阅项目的Properties
文件夹)中设置Hosting:Environment
来设置目标环境.可以在项目属性的Visual Studio中修改文件launchSettings.json
.首先应该选择个人资料"
First of all ASP.NET supports allows to set destination environment by usage ASPNET_ENV
environment variable or by setting Hosting:Environment
in launchSettings.json
file (see Properties
folder of your project). The file launchSettings.json
can be modified in Visual Studio in properties of the project. One should first choose the "Profile"
并为每个配置文件进行设置.另外,您也可以手动编辑文件Properties\launchSettings.json
.
and make setting for every profile. Alternatively one can just edit the file Properties\launchSettings.json
manually.
某些配置文件(例如hosting.json
)会使用分段自动运行.因此,可以通过例如在hosting.json
和hosting.Development.json
中指定server.urls
来设置不同的端口和不同的接口绑定.
Some configuration files, like hosting.json
works automatically using staging. Thus you can set for example different ports and different interface binding by specifying server.urls
in hosting.json
and hosting.Development.json
for example.
要在appsettings.json
中包含登台逻辑,需要在Startup.cs
中修改Startup
类的构造函数.例如:
To include staging logic in appsettings.json
one need modify the constructor of Startup
class in Startup.cs
. For example:
public class Startup
{
public static IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyContext>(options => {
options.UseSqlServer(Configuration["Data:ConnectionString"]);
})
.AddDbContext<SM9Context>(options => {
options.UseSqlServer(Configuration["Data:SM9ConnectionString"]);
});
}
}
上面的代码将配置保存在Configuration
属性中,然后使用ConfigureServices
注入MyContext
和SM9Context
数据库上下文.例如,可以创建具有所有生产性配置的主appsettings.json
文件,并创建仅覆盖一个(来自两个Data:ConnectionString
和Data:SM9ConnectionString
)连接字符串的appsettings.Development.json
文件:
The above code saves configuration in Configuration
property and then uses ConfigureServices
to make injection of MyContext
and SM9Context
database contexts. One can for example create main appsettings.json
file with all productive configuration and create appsettings.Development.json
file which overrides only one (from two Data:ConnectionString
and Data:SM9ConnectionString
) connection string:
{
"Data": {
"ConnectionString": "Server=..."
}
}
ASP.NET将结合文件appsettings.json
和可选的appsettings.Development.json
来创建完整的配置参数集.
ASP.NET will combine both files appsettings.json
and optional appsettings.Development.json
to create the full set of configuration parameters.
文章和
The article and the part of documentation describes how one can use the staging in ASP.NET 5.
这篇关于web.config中的ASP.NET 5/EF7连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!