eaglei22
eaglei22

Reputation: 2831

Asp.net MVC 4 dynamic connection string

I am learning Asp.net mvc 4 at work and modifying an asp.net mvc 4 project.

Currently, the connection to the database is set in the web.config of each project. The project uses entity framework which I am also still trying to understand (I come from a java Apache Wicket background).

What I want to do is have a property file for each environment(local, dev, test, production, etc..) and configure the connectionstring from the property file to use the database connection information affiliated for that environment.

I saw some topics on this already, Here and here. But I don't know Asp.net MVC enough to understand what C# class I should change the connection string in.

What is the best way to do this? I started off with using an external file for appSettings, but was stuck when I didn't know how to change the web.config dynamically using the appSetting key/value.

This application I am modifying has multiple projects, with multiple connection strings. I see the EF as an overloaded constructor to take a connection string, but I do not see where this constructor is used. So for now, I would just like to some how dynamically modify the connection strings. Where would I do this within the project?

Upvotes: 1

Views: 5467

Answers (1)

eaglei22
eaglei22

Reputation: 2831

For those of you looking for a solution to this issue as well, here is my solution (which is just one possible solution).

In my Web.config class I used a configSource for my connection string:

<connectionStrings configSource="connections.config"/> 

Then I created a class in my App_Start folder with a static method:

    public static void CloneEnvironmentConfigSource()
    {
        String path = AppDomain.CurrentDomain.BaseDirectory;
        Debug.WriteLine("Path = " + path);

        try
        {
            File.Copy(path + @"..\EnvironmentConfig\connections.config", path + @"connections.config", true);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            throw ex;
        }
    }

I called this method inside the Application_Start() of the Global.asax.cs file... like so:

    public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ConnectionStringBuilder.CloneEnvironmentConfigSource();

Basically I have a folder in the environment I will be "deploying" my application to which stores my configSource file to be copied into the project for the Web.config to reference.

I had to resort to this because configSource url mapping doesn't allow you to go a directory above itself. So this allows me to deploy my projects to any environment and not have to worry about changing connection string information for dev or test.

Hopefully this helps someone, and if this solution will be problematic in the future or someone thinks there will be a negative side affect, please let me know.

*** Just an update. I had an issue where Application_Start() was getting called a bunch of times and slowing down my application. I think it was because be-time the Web.config is changed at this point, the framework had already loaded the web.config, so it makes the change and reloads everything.

What I had to do was take the ConnectionStringBuilder.CloneEnvironmentConfigSource(); out of the Application_Start() and put it in the AssemblyInfo.cs as:

//Called to set config file dynamicaly used by Web.config file to set  connectionstring. 
//Has to be one of the first methods ran to avoid reload of Web.config  after changes are made.
[assembly: PreApplicationStartMethod(
  typeof(ConnectionStringBuilder), "CloneEnvironmentConfigSource")]

This way it will run before anything else.

Upvotes: 3

Related Questions