c# - Entity Framework ConnectionString won't update after changing App.Config in runtime
Entity Framework caches connection string, there isn't a method to force a refresh.
From this article: connection string given in DbContext
constructor isn't cached then you can use this as workaround:
public class MyContext : DbContext {
public MyContext()
: base(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString)
{
}
}
Save and reload app.config(applicationSettings) at runtime
You need to make a call to ConfigurationManager.RefreshSection in order to have the values re-read from disk.
Can you change the ConnectionString configuration value at runtime?
Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text;
myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
myConfiguration.Save();
Ref: http://www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-Time.aspx
Related Topics
Parsing a JSON Array Using JSON.Net
ASP.NET MVC - How to Show Unauthorized Error on Login Page
This Row Already Belongs to Another Table Error When Trying to Add Rows
Use Dbcontext in Asp .Net Singleton Injected Class
Generic List - Moving an Item Within the List
Draw on the Screen Without a Form
Dbarithmeticexpression Arguments Must Have a Numeric Common Type
Change Connection String & Reload App.Config at Run Time
Cannot Find .Cs Files for Debugging .Net Source Code
How to Make the Xmlserializer Only Serialize Plain Xml
Itemscontrol with Horizontal Orientation
How to Navigate a Few Folders Up
Winforms Issue - Error Creating Window Handle
Any Way to Turn the "Internet Off" in Windows Using C#
An Error Occurred During Report Processing. -Rldc Reporting in ASP.NET MVC