Is there a way to change connection string in database first?

落花浮王杯 提交于 2019-12-03 11:47:22

问题


I have several databases, the schema of them are same. When I use database-first, the connection string is specified when I create the edmx file. I want to know, is there a way to change the connection string? This is, so I can choose which database to operate. Thx in advance!


回答1:


Change the connection string in the web.config file.

  <connectionStrings>
    <add name="SandBoxEntities" connectionString="metadata=r... />
  </connectionStrings>

I abbreviated the actual connection string because it isn't important -- just wanted to give you an idea of what to look for in the web.config file.

You can also change your connection strings programatically. Check out Example 16.2. Programmatically modifying an EntityConnectionString.




回答2:


We do not store connection strings in our web.configs, so the accepted solution would not work for us. If you simply attempt to provide the connection string via the base DbContext constructor, you'll get the following exception:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

To resolve this, create a partial class of your context as follows and format your connection string with the additional EF metadata (where MyContext is your context model name (e.g. your model name is MyModel.edmx, than the MyContext in code below is replaced with MyModel with all three extensions .csdl, .ssdl, .msl used)):

public partial class MyContext
{
    public MyContext(string connStr)
        : base(string.Format(@"metadata=res://*/MyContext.csdl|res://*/MyContext.ssdl|res://*/MyContext.msl;provider=System.Data.SqlClient;provider connection string='{0}'", connStr))
    {
    }
}



回答3:


You can define multiple connection string in web.config and then use them in your code perhaps your job. for example:`

<connectionStrings>
  <add name="conStr1" connectionString="metadata=r... />
</connectionStrings>`

<connectionStrings>
  <add name="conStr2" connectionString="metadata=r... />
</connectionStrings>`

and so on

and your context class constructor get connection string name as parameter:

 public MyContext(string connStr)
    :  base(connStr)    {    }

Ok. now you can use in your code as below:

using (var db = new MyContext("name=conStr1"))
{
  //your code here
}

and then

using (var db = new MyContext("name=conStr2"))
{
   //your code here
}


来源:https://stackoverflow.com/questions/10508798/is-there-a-way-to-change-connection-string-in-database-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!