C # - Entity Framework ConnectionString will not be updated after changing App.Config at runtime

I am developing a WPF application that depends on the Entity Framework for accessing data. On the first installation, I need to create a new connection string based on user input, and then update App.Config accordingly.

The problem is this: after updating the App.Config Entity Framework file, it does not detect the changes and uses the old ConnectionString startup time to create the DbContext instance.

How to update Entity Framework ConnectionString parameter at runtime?

+6
source share
1 answer

Entity Framework caches the connection string, there is no way to force the update.

From in this article : the connection string specified in the DbContext constructor is not cached, you can use this as a workaround:

 public class MyContext : DbContext { public MyContext() : base(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString) { } } 
+7
source

Source: https://habr.com/ru/post/989014/


All Articles