目前在Startup中,我的sql server字符串如下所示:
public void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=servername;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection));
}
我如何使用appsettings.json中的内容:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=server;Initial Catalog=database;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
在新的ASP.NET 1.0 CORE新设置中看起来像这样看起来像这样参数化:
public void ConfigureServices(IServiceCollection services)
{
var connection2 = new SqlConnection connectionString;
services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection2));
}
另外,如果我有一个不同的测试数据库和qa,我如何知道ASP.NET应用程序为每个环境使用连接?
我的启动类已经在根目录中定义如下:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}