I manage to have different Environments in web application. In development environment I need to work on different database connection. I tried to manage on next way but unfortunately doesn't works. 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();
}
With my code I'm getting an error when I start app. An error occurred while starting the application.
General idea is when I'm in development environment to use appsettings.Development.json