【发布时间】:2017-10-11 11:53:42
【问题描述】:
我设法在 Web 应用程序中拥有不同的环境。 在开发环境中,我需要处理不同的数据库连接。 我试图管理下一条路,但不幸的是不起作用。 appsettings.Development.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=xxx.xx.xxx.xxx;Database=dbName;User Id=xxPassword=xxxxxxx;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=xxx.xx.xxx.xxx;Database=dbName;User Id=xxPassword=xxxxxxx;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
Startup.cs
public IHostingEnvironment environment;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
if (environment.IsDevelopment())
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
else if (environment.IsProduction())
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddTransient<DbInitializer>();
services.AddMvc();
}
使用我的代码启动应用程序时出现错误。 启动应用程序时出错。
一般的想法是当我在开发环境中使用 appsettings.Development.json
【问题讨论】:
标签: asp.net-core json.net .net-core