Is it possible to change an ASP.NET Web.Config value at runtime without reloading the application domain?
Is it possible to change a database connection string value in the Web.Config at runtime without reloading the application domain? The reason for doing this is that:
I am building a multi-tenanted application which uses the one code instance and multiple databases instances approach, so the 开发者_如何学Cdatabase connection string in the web.config must be able to be changed at runtime for different tenant users.
ADO.NET always injecst the database connection string in the web.config when you are using entity framework/LINQ to SQL etc, dataset etc.
So I may have to do something weird like this. Hope it is clear enough. Thanks a lot!
Yes it is possible. try this
System.Configuration.Configuration conf = WebConfigurationManager.OpenWebConfiguration(Server.MapPath);
conf.ConnectionStrings.ConnectionStrings["comp1"].ConnectionString = _connection_comp1;
conf.ConnectionStrings.ConnectionStrings["comp2"].ConnectionString = _connection_comp2;
conf.AppSettings.Settings["CompanyCode"].Value = _company_code;
conf.Save();
Use configuration sections.
http://www.kodyaz.com/articles/add-connection-string-to-web-configuration-file.aspx
精彩评论