Asp.net MVC 4动态连接字符串 [英] Asp.net MVC 4 dynamic connection string

查看:146
本文介绍了Asp.net MVC 4动态连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前,与数据库的连接已在网页中设置每个项目的.config该项目使用实体框架,我还在试图理解(我来自一个java的Apache Wicket背景)。



我想做的是为每个环境(本地,开发,测试,生产等)提供一个属性文件,并将属性文件中的connectionstring配置为使用附属于该环境的数据库连接信息。



我已经看到一些主题,这里这里。但我不知道Asp.net MVC足够了解什么C#类我应该更改连接字符串。



最好的方法是什么?我开始使用appSettings的外部文件,但是当我不知道如何使用appSetting键/值动态更改web.config时被卡住。



我正在修改的这个应用程序有多个项目,多个连接字符串。我看到EF是一个重载的构造函数来接受一个连接字符串,但是我看不到使用这个构造函数的位置。所以现在,我只想要一些如何动态修改连接字符串。在项目中我该怎么做?

解决方案

对于那些寻求此问题的解决方案,这里是我的解决方案(这只是一个可能的解决方案)。



在我的Web.config类中,我使用了一个configSource作为我的连接字符串:

 < connectionStrings configSource =connections.config/> 

然后我使用静态方法在我的App_Start文件夹中创建了一个类:

  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;
}
}

我在Application_Start()中调用了这个方法Global.asax.cs文件...像这样:

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

基本上我在环境中有一个文件夹,我将部署我的应用程序,其中存储我的configSource文件被复制到项目中以供Web.config引用。



我不得不诉诸这个原因,因为configSource url映射不允许你自己去一个目录。所以这样我可以将项目部署到任何环境中,而不用担心改变dev或test的连接字符串信息。



希望这可以帮助某人,如果这个解决方案在将来会有问题,有人认为会有负面的影响,请让我知道。



***只是一个更新。我有一个问题,其中Application_Start()被调用了一段时间,并减慢了我的应用程序。我认为这是因为在这个时候Web.config的更改,框架已经加载了web.config,所以它进行更改并重新加载所有的东西。



我必须做的是使用ConnectionStringBuilder.CloneEnvironmentConfigSource();在Application_Start()中,并将它放在AssemblyInfo.cs中:

  //调用设置配置文件动态使用Web.config文件设置connectionstring。 
//必须是第一个运行方法之一,以避免在进行更改后重新加载Web.config。
[assembly:PreApplicationStartMethod(
typeof(ConnectionStringBuilder),CloneEnvironmentConfigSource)]

这样它会在其他任何事情之前运行。


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?

解决方案

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.

这篇关于Asp.net MVC 4动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆