user69889
user69889

Reputation: 585

How to use a custom configuration class for web.config with Linq dbml classes

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!

Upvotes: 1

Views: 772

Answers (5)

Denis Troller
Denis Troller

Reputation: 7501

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.

Upvotes: 0

hunter
hunter

Reputation: 63542

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.

Upvotes: 0

James Curran
James Curran

Reputation: 103555

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.

Upvotes: 1

Lazarus
Lazarus

Reputation: 43094

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.

Upvotes: 0

Quintin Robinson
Quintin Robinson

Reputation: 82375

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.

Upvotes: 1

Related Questions