I have a need to swap between different DB instances of the same design. Is there a way to repoint the DBML to this different DB. What would be ideal would be to specify in code a different web.config connection string. However one would not want to change the Datacontext names in the codebase. If one was doing this in ADO.NET then one could choose the connection string.
Thanks,
Ed
When you instantiate a DBML the appropriate connection string is retrieved from a configuration file. (app.config
or web.config
). In the case that no setting is found, the initial connection string (which exists as a property on the DBML, and can be viewed through the designer) is used.
You will only need to use the connection string constructor if you are using multiple connection strings through a single instance, and need to specify the database to which you should connect.
In your case you simply need to include the appropriate connection string in your executing settings file. The connection string must be named correctly - see the setting which is placed in the project containing the DBML to check the name.
You can use web.config transformations and easily swap needed configurations, here are the some links :
Common Web.Config transformations with Visual Studio 2010
Web.config Transformation Syntax for Web Application Project Deployment
You should able to do that. When you generate DBML usually it writes the connection string to a configuration file (web.config or app.config). We should just change the database location in the configuration file. However, you have to make sure all your database are in Sync.
MYDataContext mycontext = new MYDataContext("Your Connection String");
There is a Constructor where you can chage the Connectionstring - EdB 2012-04-05 02:04