8
votes

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?

3
Do you have an app.config in the deployed application?Adriano Repetti
Yes. Executable.exe.configevolon
I had this problem. my changes in connectionstring,database name, didn't apply due to update edmx. I close the solution and open it again and it works.Mohammadreza

3 Answers

7
votes

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)
    {
    }
}
0
votes

Had same issue today. Connection string is stored in cached App.Config file in obj folder.

Solution: Delete obj folder.

0
votes

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