如何在运行时使用C#覆盖Web.config文件中设置的数据库?

如何在运行时使用C#覆盖Web.config文件中设置的数据库?,c#,asp.net,asp.net-mvc-4,asp.net-mvc-5,connection-string,C#,Asp.net,Asp.net Mvc 4,Asp.net Mvc 5,Connection String,我有一个上下文类,它将接受构造函数中的连接名。然后它允许我连接到一个特定的服务器 我想知道如何在运行时更改该连接的默认数据库 这就是我所做的 在Web.config文件中,我添加了以下连接字符串 <connectionStrings> <add name="BaseConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ScripterDynamo;Integrated Se

我有一个上下文类,它将接受构造函数中的连接名。然后它允许我连接到一个特定的服务器

我想知道如何在运行时更改该连接的默认数据库

这就是我所做的

在Web.config文件中,我添加了以下连接字符串

  <connectionStrings>
    <add name="BaseConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ScripterDynamo;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\ScripterDynamo.mdf" providerName="System.Data.SqlClient" />
    <add name="conn1" connectionString="Server=Serv1;Database=db1;UserId=myUser; Password=myPassword;" providerName="System.Data.SqlClient" />
    <add name="conn2" connectionString="Server=Serv2;Database=db2;UserId=myUser; Password=myPassword;" providerName="System.Data.SqlClient" />
    <add name="conn3" connectionString="Server=Serv3;Database=db3;UserId=myUser; Password=myPassword;" providerName="System.Data.SqlClient" />
    <add name="conn4" connectionString="Server=Serv4;Database=db4;UserId=myUser; Password=myPassword;" providerName="System.Data.SqlClient" />
  </connectionStrings>
这里唯一的问题是连接“conn4”将连接到名为db4的数据库,该数据库在连接字符串中默认设置。在大多数情况下,这是意料之中的事情。但在我的例子中,我希望能够在运行时更改要连接到的数据库

我的问题是,如何在运行中更改数据库?有没有办法在上下文类中添加一个只覆盖默认数据库的方法

已更新

下面是我更改上下文类的内容,希望setDatabase方法允许我在运行时更改数据库

using ScripterEngine.Models;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace ScripterEngine.DataAccessLayer
{
    public class BaseContext : DbContext
    {
        protected string connectionName;
        public DbSet<Campaign> Campaign { get; set; }
        public DbSet<TableMeta> TableMeta { get; set; }

        public BaseContext(string connName = "BaseConnection")
            : base(connName)
        {
            connectionName = connName;
        }

        public BaseContext setDatabase(string databaseName)
        {
            var connection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;

            //change the database before creating the new connection
            return new BaseContext(connection);
        }


    }
}

您可以这样做,这个示例Data只是一个需要理解的示例,您可以在此处对所需的值进行协商,连接字符串保持如下状态:

<connectionStrings>
        <add name="ConnectionString.Properties.Settings.CustomConnectionString"
            connectionString="Persist Security Info=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
有了这个,你可以改变一切礼仪

编辑: 看看你的变化我想你可能需要

但是我认为这不是一个好的实践,为什么不简单地创建许多连接字符串呢


希望此帮助

您可以这样做,此示例数据只是一个需要理解的示例,您可以在此处输入所需的值,连接字符串保持如下状态:

<connectionStrings>
        <add name="ConnectionString.Properties.Settings.CustomConnectionString"
            connectionString="Persist Security Info=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
有了这个,你可以改变一切礼仪

编辑: 看看你的变化我想你可能需要

但是我认为这不是一个好的实践,为什么不简单地创建许多连接字符串呢

希望这有助于

尝试设置属性

dataContextInstance.Database.Connection.ConnectionString = "new connection string";
但我不确定您在更改连接字符串后是否不需要重新初始化某些属性/代码。

尝试设置属性

dataContextInstance.Database.Connection.ConnectionString = "new connection string";

但我不确定在更改连接字符串后是否不需要重新初始化某些属性/代码。

我创建了一个2上下文

  • BaseContext:此上下文不会更改,我使用它使用代码优先的方法设计我的应用程序

  • DynamicContext:此上下文将在运行时更改

  • 为了使DynamicContext正常工作,我在连接文件中创建了一个动态连接

    我基本上把这个添加到我的块中

    <add name="DynamicConnection" connectionString="Server=DefaultServer;Initial Catalog=I3_IC;User ID=defaultUser; Password=defaultPassword;" providerName="System.Data.SqlClient" />
    
    那么我就这样用这个

    //Create a dynamic context, note that you can also change the username and the password of the default user by using the second and third parameters.
    var dbDynamic = new DynamicContext(serverName);
    
    //Finally, you can set the correct table
    var conn1 = dbDynamic.setDatabase('table1');
    
    //If you want to connect to a different table
    var conn2 = dbDynamic.setDatabase('table2');
    

    我创建了一个2个上下文

  • BaseContext:此上下文不会更改,我使用它使用代码优先的方法设计我的应用程序

  • DynamicContext:此上下文将在运行时更改

  • 为了使DynamicContext正常工作,我在连接文件中创建了一个动态连接

    我基本上把这个添加到我的块中

    <add name="DynamicConnection" connectionString="Server=DefaultServer;Initial Catalog=I3_IC;User ID=defaultUser; Password=defaultPassword;" providerName="System.Data.SqlClient" />
    
    那么我就这样用这个

    //Create a dynamic context, note that you can also change the username and the password of the default user by using the second and third parameters.
    var dbDynamic = new DynamicContext(serverName);
    
    //Finally, you can set the correct table
    var conn1 = dbDynamic.setDatabase('table1');
    
    //If you want to connect to a different table
    var conn2 = dbDynamic.setDatabase('table2');
    

    所以你想用你的“conn4”连接数据库“db3”?@MarcoPalma正确。我还用一个起点更新了我的问题,您可以帮助我更改数据库,以便您想连接“conn4”,例如数据库“db3”?@MarcoPalma正确。我还用一个起点更新了我的问题您可以帮助我更改数据库我真的不想将数据库字符串管理到多个位置。我需要通过给定的connectionstring名称检索连接字符串。然后更改数据库名称。请看一下我的最新问题。我在底部添加了一些代码,我真的不想将数据库字符串管理到多个位置。我需要通过给定的connectionstring名称检索连接字符串。然后更改数据库名称。请看一下我的最新问题。我在底部添加了一些代码
    //Create a dynamic context, note that you can also change the username and the password of the default user by using the second and third parameters.
    var dbDynamic = new DynamicContext(serverName);
    
    //Finally, you can set the correct table
    var conn1 = dbDynamic.setDatabase('table1');
    
    //If you want to connect to a different table
    var conn2 = dbDynamic.setDatabase('table2');