Working on EF4-based project. In my app I change named connection string, used by ObjectContext, and save it back to App.config. But ObjectContext saves it's connection and does not mind about changes. Trying to reinit it with manually created connection, but it doesn't work. I have ConnectionManager for that - here it's Save
method:
/// <summary>
/// Save configuration changes
/// </summary>
public void Save()
{
// Apply connection string
config.ConnectionStrings.ConnectionStrings[connectionStringName].ConnectionString = entityConnectionStringBuilder.ConnectionString;
// Save config
config.Save();
// Apply changes to context
EntityConnection newConnection = new EntityConnection(entityConnectionStringBuilder.ConnectionString);
Entities context = new Entities(newConnection);
// Entities context = new Entities(config.ConnectionStrings.ConnectionStrings[connectionStringName].ConnectionString);
context.Dispose();
}
So, is there any magic to persistently apply new connection to ObjectContext without restarting a program or may be I should reload App.Config?
NOTE: All ObjectContext
usages are wrapped in Unit-Of-Work pattern, so it is Dispose
d each time.
You need to be careful about changing app.config at runtime.
If you are testing in VS check which file is actually changed. app.config is copied on debug start.
Furthermore app.config is cached. so unless you
you wont have much luck...
Suggest to google around on app.Config changes if still having issues.
But im Not sure if you can make EF will read runtime added named connection strings. If not, dynamic connection string may also be something you need to look at. EntityFramework code-first custom connection string and migrations