c# - Entity Framework ConnectionString won't update after changing App.Config in runtime

app-config c# connection-string entity-framework entity-framework-6

Question

I'm developing a WPF application which depends on Entity Framework for data access. At the first time installation I need to create a new connection string based on the User input, then updating App.Config according to that.

The problem is: after updating the App.Config file, Entity Framework doesn't detect the change and uses the old startup-time ConnectionString for instantiating the DbContext.

How can I update the Entity Framework's ConnectionString setting at runtime?

1
8
6/12/2015 2:33:36 PM

Accepted Answer

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)
    {
    }
}
7
6/12/2015 2:29:39 PM

Popular Answer

I had the same error. My new connection string was missing "Initial Catalog="



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow