Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 612
James Avatar asked May 09 '12 01:05

James


People also ask

How do I change the database connection in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

What is a connection string in database?

A connection string is a string that contains information about a data source (usually a database engine), as well as the information necessary to connect to it.

Where is SQL connection string stored?

Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file.

What is database first approach in Entity Framework?

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.


2 Answers

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))
    {
    }
}
like image 169
rjchicago Avatar answered Oct 02 '22 13:10

rjchicago


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.

like image 28
Jon Crowell Avatar answered Oct 02 '22 13:10

Jon Crowell