views:

138

answers:

3

Hi,

I develop an asp.net web application and I use ASP.NET membership provider. The application uses the membership schema and all required objects inside main application database However, during development I have to switch to various databases, for different development and testing scenarios. For this I have an external connection strings section file and also an external appsettings section, which allow me to not change main web.config but switch the db easily, by changing setting only in appsettings section.

My files are as below:

<connectionStrings configSource="connections.config">
</connectionStrings>

<appSettings file="local.config">
    ....

ConnectionStrings looks as usual:

<connectionStrings>
  <add name="MyDB1" connectionString="..." ... />
  <add name="MyDB2" connectionString="..." ... />
  ....
</connectionStrings>

And local.config as below

<appSettings>
    <add key="ConnectionString" value="MyDB2" />

My code takes into account to use this connection string

But membership settings in web.config contains the connection string name directly into the setting, like

<add name="MembershipProvider" connectionStringName="MyDB2" ...>
....
<add name="RoleProvider" connectionStringName="MyDB2" ...>

Because of this, every time I have to edit them too to use the new db.

Is there any way to config membership provider to use an appsetting to select db connection for membership db? Or to "redirect" it to read connection setting from somewhere else? Or at least to have this in some external file (like local.config)

Maybe is some easy way to wrap asp.net membership provider intio my own provider which will just read connection string from where I want and pass it to original membership provider, and then just delegate the whole membership functionality to asp.net membership provider.

Thanks.

+1  A: 

I use different external configuration files for connection strings in this situation. Possibly (depending on your concrete situation) you can use Visual Studios pre build events to copy the files to the right places.

Dirk
Thanks, but this isn't exactly what I'm looking for, since the app is also deployed multiple times, on different sites, and I would also want to customize it for each site w/o changing web.config - just by changing the local.settings file (which is per-instance)
bzamfir
There is no change to web.config! You replace the external configuration files. You can address deployment to different sites with this method, by putting your external configuration files into the App_Data folder and merging them with the runtime settings in the deployment process.
Dirk
Any suggestion on how to automatically "merge" specific settings at deployment with general settings? Or do I have to do that manually?
bzamfir
Maybe you could use different configuration sections for variable and invariable settings. External configuration files with invariable settings are included in the project root (or somewhere else). Configuration files with variable settings are included in App_Data. Publish your project without files from App_Data. That would do the trick.
Dirk
A: 

Actually, after some more thinking, I found a solution for my problem.

My initial thought was to set specific connection string name in local.config, which will point to a connection string in conenction string sections. But with this approach I had to change connection string name also in membership-related settings

The new approach which helped me to have a single reference to connection string (to be used by botrh my DAL and membership) was the following:

  1. in web.config I have a setting which is used by my DAL, which store the name of the connection string:

    <add key="ConnectionString" value="ManagDB" />

  2. membership-related settings have reference to the same connection string

    <add name="MembershipProvider" connectionStringName="ManagDB" ...>
    <add name="RoleProvider" connectionStringName="ManagDB" ...>

Web.config will be tyhe same for all instances of my appliucation, so no change is needed in it

  1. I extract the connections strungs into an external file, with this setting in web.config

    <connectionStrings configSource="connections.config"> </connectionStrings>

  2. Then I define the specific connection string for a certain instance in file connections.config

    <connectionStrings>
    <add name="MyDB" connectionString="Data Source=..." providerName="System.Data.SqlClient" /> </connectionStrings>

Using this approach I was able to have a single, unchangeable web.config specific to the application, common to all instances, and then specific connection string to a certain instance which allow both DAL and membership to use the same database

bzamfir
+1  A: 

I found this solution to create inherited classes and override the Initialize() method to change the connection string. Worked well for me but the web.config settings can be a little tricky. I ended up having to create both a SqlMembershipProvider and a SqlRoleProvider.

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/260d8536-c39f-41ec-b181-4d452cf054b3

brendan
Thanks, I'll give it a try
bzamfir