views:

436

answers:

5

I have a web project that uses a custom configuration class to store app settings in web.config. I do this to be able to store and access configuration settings in web.config that are based on server name. That way when the project moves from development, to staging, to production, I don't have to remember to change the web.config settings like connection string and other settings that may be different from server to server. The classes will retrieve the correct config settings from web.config based on the server it is deployed on.

This works rather well. However, now I converted the project to use Linq using the generated dbml classes. This is great, but the generated classes insist on using a simply connection string from web.config. Since these classes are automatically generated, I cannot simply change the code to use my custom config classes. Can someone explain how it would be possible to have the generated classes call my custom object to retrieve the proper connection string?

Thanks!

+1  A: 

It would be a better practice to explicitly provide the connection string to the DataContext constructor. That way your app can grab the correct connection string via your webconfig helper and the datacontext can always be provided with the absolutely correct connection string. Rather then relying on the Context to select the appropriate one from the webconfig.

Quintin Robinson
This is the most straight forward way. I should have realized the context object accepts the connection string as a parameter. Thanks!
No problem, I only know because I went through something very similar, good luck!
Quintin Robinson
A: 

I don't have the answer but you might want to take a look at the example application StockTrader by Microsoft which implements a central configuration service and I believe uses Linq. I've not looked at it in a long time but it could provide you with the insight you are seeking.

Sorry that I've no direct answer.

Lazarus
+1  A: 

An alternative:

The <connectionStrings> element of the web.config file can take a configSource attribute, which specifies the name of an XML file which holds the data for that section. I set it up as:

 <connectionStrings configSource="connections.config" />

The put a distinct connections.config with the info specific to the environment on each PC.

James Curran
A: 

Seems like you already got an answer, but I thought I'd offer this up:

public MyDataContext() : 
  base(global::System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString, mappingSource)
{
 OnCreated();
}

You could manually modify your .designer.cs partial class file and override that default Context constructor with your own.

hunter
A: 

You can simply pass the connection string as a parameter to the constructor of your DataContext class.

In that case, just retrieve the connection string the way you used to before from your configuration and hand it over when creating a DataContext.

Denis Troller