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

4

9

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?

Shiri answered 12/6, 2015 at 14:7 Comment(3)
Do you have an app.config in the deployed application?Vicarage
Yes. Executable.exe.configShiri
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.Sword
V
8

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)
    {
    }
}
Vicarage answered 12/6, 2015 at 14:29 Comment(4)
Thanks, Very useful link by the way. But based on the comments written at the above of the document that says "Manual changes to this file will be overwritten if the code is regenerated.", won't the changes fade away later?Shiri
If you recompile your application...yes but it's not something that will happen on a deployed application.Vicarage
Make sure to add: "using System.Configuration" to the namespace.Smetana
This is just a hack not a real solution and you have to reset it every time you rebuild your solution.....Cabriolet
A
0

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

Solution: Delete obj folder.

Axe answered 5/2, 2020 at 14:56 Comment(0)
R
0

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

Runstadler answered 26/3, 2020 at 15:46 Comment(0)
C
0

Global.cs

public class Global
   {
    public static string ConnectionString=String.Empty;
   }

Store connection string as temporary in Global.cs first time after connection string saved in App.config.

DBContext.cs

 public class DBContext: DbContext
    {
        public DbSet<User> UserInfo { get; set; }   
        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                String.IsNullOrEmpty(Global.ConnectionString) ? ConfigurationManager.ConnectionStrings["your_connectionstring"].ConnectionString
                : Global.ConnectionString
                );
        }

    } 

Firstly, you have already defined connection string in your App.config. Hope you enjoy!

Castled answered 1/10, 2022 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.